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

this is assigment2, i need help for assignment 2: Write a recursive descent comp

ID: 3814113 • Letter: T

Question

this is assigment2, i need help for assignment 2:

Write a recursive descent compiler for the following grammar: http://stackoverflow.com/questions/22529757/c-parser-using-lex-and-yacc BEGIN END := | ; | + | - | * | DIV | MOD | | | ( ) Here, represents any valid sequence of characters and digits starting with a character; and represents any valid integer literal. “^” is the exponentiation operator. Since is the start symbol, you may assume that a valid program consists of zero or more statements enclosed by “BEGIN” and “END”. You will, of course, find it necessary to remove left recursion in certain productions. The output of your compiler will be assembly code for the abstract stack machine you used in programming assignment #

1. , you will only be using only a small subset of the opcodes. You’ll have to make two passes through the code; one to generate the symbol table in order build the data section of your output .asm file and the second to generate the code section. You are to write a lexical analyzer function yylex() using lex which you can then call from your C program. On errors, a “meaningful” error message should be printed; i.e., “invalid token in line xx”, “expression expected”, etc. Management would be impressed if you attempted to recover from an error rather than simply halting compilation. Submit your source files for your compiler and lexical analyzer, along with documentation on how to build and execute your compiler. You should, of course test your solution by sending the output to your assembler and validating that the code produced is correct.

below is the assignment 1, question,

Your first assignment is to use ad-hoc methods (partially described in the assignment document) to write a two-pass assembler for an abstract stack machine.

Attached also are four files:

a source file, lab1.asm, and

a binary file, lab1.bin, which is the output of the assembler when given the previous source.

a screen grab of a hexdump of the output file

a very much simplified version of the assembler written in Java. It also lacks the symbol table and symbol table entry class definitions.

clues;

Explanation / Answer

private Symbol parseEPrime() throws IOException { NonterminalSymbol ePrime = new NonterminalSymbol("E'"); TerminalSymbol op = lexer.peek(); // look ahead if (op == null) { // end of input: epsilon production is applied System.out.println("end of input"); } else { if (!op.getValue().equals("+") && !op.getValue().equals("-")) { // we saw a terminal symbol other than + or -: // apply epsilon production } else { // consume the operator lexer.get(); // saw + or - // production is // E' -> + T E' // or // E' -> - T E' ePrime.addChild(op); ePrime.addChild(parseT()); ePrime.addChild(parseEPrime()); } } return ePrime; }