Answered step by step
Verified Expert Solution
Question
1 Approved Answer
There are two kinds of implementation of LL top - down parsing: recursive descent and table driven. 1 . Recursive Descent Parser ( textbook 2
There are two kinds of implementation of LL topdown parsing: recursive descent and table driven.
Recursive Descent Parser textbook
A recursive descent parser RDP is a parser that constructs a parse tree from the root down, predicting at
each step which production will be used to expand the current node, based on the next available token of
input. It also reports any syntactical errors if any. In RDP each subroutine corresponds, one to one, to a
nonterminal of the grammar. Example on page illustrates how RDP parses a sum and average
program in simple calculator language.
Figure shows the pseudocode of RDP for calculator language. It verifies that a program is
syntactically correct ie that none of the otherwise parseerror clauses in the case statements are
executed and that match always sees what it expects to see The basic implementation of the parser for
the calculator language, called parserc is given.
Corresponding to Figure there are ten functions in parser.c as follows:
void program;
void stmtlist;
void stmt;
void expr;
void termtail;
void term;
void factortail;
void factor;
void addop;
void multop;
Table Driven Parser textbook
In table driven topdown parser, it requires a stack and parse table to do the same job as RDP The parser
iterates around a loop in which it pops the top symbol off the stack and performs the following actions. If
the popped symbol is a terminal, the parser attempts to match it against an incoming token from the
scanner. If the match fails, the parser announces a syntax error. If the popped symbol is a nonterminal, the
parser uses that nonterminal together with the next available input token to index into the twodimensional
table that tells it which production to predict or whether to announce a syntax error
The scannerc is given and is partially working. First, it only checks whether the first token in an input
string of tokens is valid or not. Second, it does not check for comments. That is it should check if the
comments have the correct forms either or following the regular expression we discussed in
class and in Project
Task Description
Your task is to implement a parser using tabledriven parsing, using C programming language, called
parserc that works correctly with scannerc
When executing your programs, it can take a calculator language program with comments and find if it
has lexical errors and syntax errors or not.
Please compile both programs by typing
gcc parser.c scanner.c o parser
To run the program, the user will type at command line:
parser prog
where prog is the name of the calculator program to be checked input by the user.
Next, your program should output error messages indicating any lexical or syntax error in the
calculator program; otherwise, output a No lexical and syntax error. message.
For example, below calculator program below, called prog is both lexically and syntactically
correct:
prog
read a
read b
area : a b
perimeter :a b
write area
write perimeter
Below program, called prog is lexically incorrect.
prog
read a
read b
area : a b
perimeter :a b
write area
write perimeter
The below program, called prog is lexically correct but syntactically incorrect.
prog
read a
read b
area : a b
perimeter :a b
write area
write perimeter
The below programs, called prog and prog respectively, are lexically correct but syntactically
incorrect.
prog
read a b
area : a b
perimeter :a b
write area
write perimeter
and
prog
read a
read b
area : a b
perimeter :a b
write area
write perimeter
If you have more test programs, please name them progprog etc..
Please make sure that the error messages both lexical and syntactical are detailed and informative by showing which lines of the program has errors
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