Parser that will recognize and evaluate an arithmetic expression generated by the given context-free grammar G as follows

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

USE PROGRAMMING LANGUAGE C++

Build a Parser that will recognize and evaluate an arithmetic expression generated by the given context-free grammar G as follows, in which int is a terminal symbol representing integers.


E → E + T | E – T | -E | T
T → T * F | T/F | F
F → int | (E)


Basically, we want the parser that will:
1. produce an error message if its input arithmetic expression is not in the language of the given grammar.
2. implement a calculator to compute the result of the input arithmetic expression if the input arithmetic expression is in the language of the given grammar (which is called a valid arithmetic expression).


Each input arithmetic expression in the language will have a single parse tree based on the following precedence and associativity rules:
• Unary minus (-) have precedence over all operators.
• Parentheses have precedence over the binary operators *, /, +, and -.
• * and / have precedence over the binary operators + and -.

 

Write your own program (including lexer and parser) to recognize and evaluate the input arithmetic expressions generated by given G using recursive descent parsing (a technique known as top-down LL(1) parsing). The idea of recursive-descent parsing is to transform each nonterminal of a grammar into a subroutine that will recognize exactly that nonterminal in the input. Notice that, left recursive grammars, such as the given G, are unsuitable for recursive-descent parsing because a left-recursive production leads to an infinite recursion (loop). While the parser may be partially correct, it may not terminate. We need to transform G to an equivalent non-left-recursive grammar G’ by eliminating left recursions in G. Also, the language described by G’ is the same as that of grammar G, that is, L(G’) = L(G) and G’ is unambiguous, and each choice can be made by looking at the next token in the input string. You must write the program in C++.

 


Requirement for submission: your final submission to Canvas must have two (2) files: your executable program (with necessary comments), and a readme file including the rewritten grammar G’ and a brief manual for code (e.g., how to compile and run the program).

Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 3 steps

Blurred answer
Knowledge Booster
Sorting
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education