Please help me with the coding for LL(1)!! The given grammar was: P PL | L L N;
ID: 3839817 • Letter: P
Question
Please help me with the coding for LL(1)!!
The given grammar was:
P PL | 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
And the grammar transformed in LL(1) is:
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.
1. T all clad V 0 1 2 3 E CEOE) C' else (P) E 8, N' print M 10, L N IC 12.Explanation / Answer
A content record containing charges which could have been written straightforwardly into the shell.
There is no distinction in grammar between intuitive summon line utilize and putting the charges in a record. A few charges are just helpful when utilized intuitively (e.g. charge line history review) and different summons are excessively perplexing, making it impossible to utilize intuitively.
The shell itself has constrained abilities - the power originates from utilizing it as a "paste" dialect to join the standard Unix utilities, and custom programming, to create an apparatus more valuable than the segment parts alone.
Any shell can be utilized for composing a shell script. To take into consideration this, the principal line of each script is:
#!/way/to/shell (e.g. #!/canister/ksh).
The #! characters advise the framework to find the accompanying pathname, begin it up and bolster it whatever is left of the document as information. Any program which can read charges from a record can be begun up along these lines, the length of it perceives the # remark tradition. The program is begun, and afterward the script record is given to it as a contention. Along these lines, the script must be coherent and additionally executable. Illustrations are perl, awk, tcl and python.
Any document can be utilized as contribution to a shell by utilizing the linguistic structure:
ksh myscript
In the event that the record is made executable utilizing chmod, it turns into another summon and accessible for utilize (subject to the typical $PATH seek).
chmod +x myscript
A shell script can be as straightforward as a succession of charges that you write frequently. By placing them into a script, you decrease them to a solitary order.
Illustration: ex0 show, content
1: #!/receptacle/sh
2: date
3: pwd
4: du - k
(2)
Why utilize Shell Scripts
Join protracted and redundant successions of summons into a solitary, straightforward order.
Sum up a succession of operations on one arrangement of information, into a method that can be connected to any comparative arrangement of information.
(e.g. apply a similar investigation to each information document on a CD, without expecting to rehash the orders)
Make new summons utilizing blends of utilities in ways the first creators never considered.
Basic shell scripts may be composed as shell monikers, yet the script can be made accessible to all clients and all procedures. Shell nom de plumes apply just to the present shell.
Wrap programs over which you have no control inside a situation that you can control.
e.g. set condition factors, change to a unique registry, make or select a setup document, divert yield, log use, and afterward run the program.
Make modified datasets on the fly, and call applications (e.g. matlab, sas, idl, gnuplot) to deal with them, or make redid application charges/strategies.
Quick prototyping (yet abstain from giving models a chance to wind up generation)
Ordinary employments
Framework boot scripts (/and so forth/init.d)
Framework chairmen, for computerizing numerous parts of PC upkeep, client account creation and so forth.
Application bundle establishment apparatuses
Different apparatuses may make fancier installers (e.g. tcl/tk), yet can not be thought to be introduced as of now. Shell scripts are utilized on the grounds that they are exceptionally convenient. Some product accompanies an entire establishment of the device it needs to utilize (tcl/tk/python) with a specific end goal to act naturally contained, however this prompts programming bloat.
Application startup scripts, particularly unattended applications (e.g. begun from cron or at)
Any client expecting to robotize the way toward setting up and running business applications, or their own code