Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

LL1 parser coding... please.. P P L | L L N; | M; | C N print E M print \"W\" W

ID: 3839728 • Letter: L

Question

LL1 parser coding... please..

P P L | L

L N; | M; | C

N print E

M print "W"

W TW |

C if E {P} | if E {P} else {P}

E (EOE) | V (note: this has a variable O)

O + | - | *

V 0 | 1 | 2 | 3 (note: this has a terminal 0 (zero))

T a | b | c | d

5.1 Parsing strings with an LL(1) table driven parser

Implement a program which parses strings using an LL(1) table driven parser using the table you determined for G in the previous exercise.

You may use Python, Java, C, C++, or Haskell. If you’d like to use a different language then please check with us first.

• Input: The first command line argument is the filename of a file containing the string of characters to test.

• Output: 1. Print a trace of the execution, showing the steps followed by the program as it performs the left-most derivation. This should look similar to parsing the string through a PDA. An example of this is given in the appendices.

2. After parsing the whole input file, print ACCEPTED or REJECTED, depending on whether or not the string could be derived by the grammar.

3. If there is a symbol in the input string which is not a terminal from the grammar, the program should output ERROR_INVALID_SYMBOL (This could be during or before trying to parse the input.) Note: all whitespace in the input file should be ignored (line breaks, spaces, etc.) The output will be easier to read if you remove the whitespace before starting the parse. Examples of the program output syntax are provided in the appendices.

5.2 Evaluating programs written in G

If a second command line argument “eval” is given, then instead of printing the trace of the parse, your program should:

1. Build a parse tree as it performs the leftmost derivation

2. Evaluate that parse tree. The semantics (meaning) which we are applying to our rules are as follows:

• V variables derive integers • W variables derive strings • E expressions are evaluated like normal integer arithmetic

• print E statements output (to screen) the result of evaluating the expression E. (i.e. print (1+1) outputs 2)

• print "W" statements output the string derived from W (i.e. print "abba" outputs abba)

• if statements evaluate the contents of their if block if and only if the condition evaluated to a non-zero value, otherwise the else block is evaluated instead (if there is one). If the input could not be parsed then output REJECTED instead. Some examples of programs and their expected output are provided in the appendices.

Explanation / Answer

The parser's assignment is to decide whether the information program, spoken to by the
stream of arranged words delivered by the scanner, is a legitimate sentence in the
programming dialect. To do as such, the parser endeavors to fabricate an induction
for the information program, utilizing a sentence structure for the programming dialect.
This section presents setting free sentence structures, a documentation used to determine
the linguistic structure of programming dialects. It builds up a few procedures for
finding an induction, given a sentence structure and an information program.
Catchphrases: Parsing, Grammar, ll(1), lr(1), Recursive Descent
3.1 INTRODUCTION
Parsing is the second phase of the compiler's front end. The parser works
with the program as changed by the scanner; it sees a flood of words
where each word is commented on with a syntactic class (similar to its part
of discourse). The parser determines a syntactic structure for the program, fitting
the words into a syntactic model of the source programming dialect.
In the event that the parser discovers that the info stream is a legitimate program, it assembles a
solid model of the program for use by the later periods of accumulation. On the off chance that
the info stream is not a legitimate program, the parser reports the issue and
fitting analytic data to the client.
As an issue, parsing has numerous similitudes to filtering. The formal prob-
lem has been examined broadly as a major aspect of formal dialect hypothesis; that
work frames the hypothetical reason for the pragmatic parsing procedures utilized as a part of
generally compilers. Speed matters; the greater part of the systems that we will consider take
time corresponding to the measure of the program and its portrayal. Low-
level detail influences execution; a similar usage tradeoffs emerge