Question: Write a generic table-driven predictive parser in Prolog. The parser will be instantiated with the LL(1) grammar for arithmetic expressions with operators - (minus) and
Write a generic table-driven predictive parser in Prolog. The parser will be instantiated with the LL(1) grammar for arithmetic expressions with operators - (minus) and * (times). Given an expression as input, the parser will parse the expression, producing as a result the sequence of prductions applied. In addition, the parser will interpret the expression during the predictive parse computing the expression value.The LL(1) grammar for arithmetic expressions with operators - and * is as follows:
0.start expr
1.expr term term_tail
2.term_tail - term term_tail
3.term_tail
4.term num factor_tail
5.factor_tail * num factor_tail
6.factor_tail
a. Write 'transform' which translates a token stream into the generic representation.
eg transform([4,-,15],R).
R = [term(num,4),term(minus,_),term(num,15)].
b.Write parseLL(R,ProdSeq) which takes a transformed list R and produces the production sequence the predictive parser applies.
eg transform([3,-,5],R),parseLL(R,ProdSeq).
ProdSeq = [0, 1, 4, 6, 2, 4, 6, 3].
c.Write Solve, which augments parseLL with computation. eg., transform([3,-,5],R),Solve(R,ProdSeq,V). ProdSeq = [0, 1, 4, 6, 2, 4, 6, 3], V = -2.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
