Answered step by step
Verified Expert Solution
Link Copied!

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
1. Transformed Grammar into LL(1):
1.1. Remove all the EBNF notations and replace them by right-recursive list-generating productions.
1.2. Analyze the syntactical definition. List in your documentation all the ambiguities and left recursions or any error you may have found in the grammar.
1.3. Modify the productions so that the left recursions and ambiguities are removed without modifying the language.
1.4. Include in your documentation the set of productions that can be parsed using the top-down predictive parsing method, i.e. an LL(1) grammar.
2. FIRST and FOLLOW sets:
2.1. Derive the FIRST and FOLLOW sets for each non-terminal in your transformed grammar.
3. Design:
3.1. 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.
4. Implementation Tools:
4.1. Identify all the tools/libraries/techniques 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 table-driven) 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.
Error-Reporting: 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 analysis/transformation 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'.
Non-terminals are represented in italics, thisWay.
The empty phrase is represented by EPSILON.
EBNF-style repetition notation is represented using curly brackets {This way}. It represents zero or more occurrence of the sentential form enclosed in the brackets.
EBNF-style optionality notation is represented using square brackets [This way]. It represents zero or one occurrence of the sentential form enclosed in the brackets.
The non-terminal 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 ->{classDecl}{funcDef} 'main' funcBody ';'
classDecl -> 'class' 'id'[':''id'{',''id'}]'{'{varDecl}{funcDecl}'}'';'
funcDecl -> type 'id''(' fParams ')'';'
funcHead -> type ['id''sr']'id''(' fParams ')'
funcDef -> funcHead funcBody ';'
funcBody ->'{'{varDecl}{statement}'}'
varDecl -> type 'id'{arraySize}';'
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 ->{idnest}'id'{indice}
functionCall ->{idnest}'id''(' aParams ')'
idnest ->'id'{indice}'.'
|'id''(' aParams ')''.'
indice ->'[' arithExpr ']'
arraySize ->'[' 'intNum' ']'
type -> 'integer' | 'float' |'id'
fParams -> type 'id'{arraySize}{fParamsTail}| EPSILON
aParams -> expr {aParamsTail}| EPSILON
fParamsTail ->',' type 'id'{arraySize}
aParamsTail ->',' expr
assignOp ->'='
relOp ->'eq'| 'neq' |'lt'|'gt'| '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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Oracle Database Foundations Technology Fundamentals For IT Success

Authors: Bob Bryla

1st Edition

0782143725, 9780782143720

More Books

Students also viewed these Databases questions

Question

How would you handle the difficulty level of the texts?

Answered: 1 week ago

Question

1. What are your creative strengths?

Answered: 1 week ago

Question

What metaphors might describe how we work together?

Answered: 1 week ago