Answered step by step
Verified Expert Solution
Question
1 Approved Answer
design and implement a syntax analyzer for the language specified by the grammar specified on the next page. Deliverables: Documentation 1 . Transformed Grammar into
design and implement a syntax analyzer for the language specified by the grammar specified on the next page.
Deliverables:
Documentation
Transformed Grammar into LL:
Remove all the EBNF notations and replace them by rightrecursive listgenerating productions.
Analyze the syntactical definition. List in your documentation all the ambiguities and left recursions or any error you may have found in the grammar.
Modify the productions so that the left recursions and ambiguities are removed without modifying the language.
Include in your documentation the set of productions that can be parsed using the topdown predictive parsing method, ie an LL grammar.
FIRST and FOLLOW sets:
Derive the FIRST and FOLLOW sets for each nonterminal in your transformed grammar.
Design:
Give a brief overview of the overall structure of your solution, as well as a brief description of the role of each component of your implementation.
Implementation Tools:
Identify all the toolslibrariestechniques that you have used in your analysis or implementation and justify why you have used these particular ones as opposed to others.
Implementation
Parser: Implement a predictive parser recursive descent or tabledriven for your modified set of grammar rules. The result of the parser should be the creation of a tree data structure representing the parse tree as identified by the parsing process. This tree will become the intermediate representation used by the two following assignments.
Derivation Output: Your parser should write to a file the derivation that proves that the source program can be derived from the starting symbol.
ErrorReporting: Parser should properly identify all errors in the input program and print a meaningful message to the user for each error encountered. The error messages should be informative on the nature of the errors, as well as the location of errors in the input file.
Error Recovery: The parser should implement an error recovery method that permits to report all errors present in the source code.
Test Cases: Write a set of source files that enable to test the parser for all syntactical structures involved in the language. Include cases testing for a variety of different errors to demonstrate the accuracy of your error reporting and recovery.
Note that the grammar analysistransformation process can be greatly simplified by the use of tools such as the kfgEdit AtoCC tool.
Grammar:
The syntactical definition is using the following conventions:
Terminals lexical elements, or tokens are represented in single quotes 'thisWay'.
Nonterminals are represented in italics, thisWay.
The empty phrase is represented by EPSILON.
EBNFstyle repetition notation is represented using curly brackets This way It represents zero or more occurrence of the sentential form enclosed in the brackets.
EBNFstyle optionality notation is represented using square brackets This way It represents zero or one occurrence of the sentential form enclosed in the brackets.
The nonterminal is the starting symbol of the grammar.
Except from the EBNF constructs, the grammar is expressed using the syntax used by the kfgEdit AtoCC toolkit application.
prog classDeclfuncDef 'main' funcBody ;
classDecl 'class' id:ididvarDeclfuncDecl;
funcDecl type id fParams ;
funcHead type idsrid fParams
funcDef funcHead funcBody ;
funcBody varDeclstatement
varDecl type idarraySize;
statement assignStat ;
if expr 'then' statBlock 'else' statBlock ;
'for' type id assignOp expr ; relExpr ; assignStat statBlock ;
'read' variable ;
'write' expr ;
'return' expr ;
assignStat variable assignOp expr
statBlock statement statement EPSILON
expr arithExpr relExpr
relExpr arithExpr relOp arithExpr
arithExpr arithExpr addOp term term
sign
term term multOp factor factor
factor variable
functionCall
'intNum' 'floatNum'
arithExpr
'not' factor
sign factor
variable idnestidindice
functionCall idnestid aParams
idnest idindice
id aParams
indice arithExpr
arraySize 'intNum'
type 'integer' 'float' id
fParams type idarraySizefParamsTail EPSILON
aParams expr aParamsTail EPSILON
fParamsTail type idarraySize
aParamsTail expr
assignOp
relOp eq 'neq' ltgt 'leq' 'geq'
addOp or
multOp 'and'
Tokens
Keywords: main class
if then else for read write return
integer float
Operators: eq
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started