Write a recursive descent parser routines for <expr>, <term>, and <factor> given in Section 4.4.1 (see below) using python.
The EBNF grammar in Section_4.4.1 is as follows
<expr> → <term> {(+ | -) <term>}
<term>→ <factor> {(* | /) <factor>}
<factor>→ id | int_constant | ( <expr> )
class EquationInterpreter(object):
"""Equation interpreter:
Grammar:
equation := expression '=' expression
expression := ['-'] term { (+|-) term }
term := number | variable
number := digit {digit}
digit := 0-9
variable := [number] 'x'
"""
def __init__(self, equation):
self.next_index = 0
self.equation = equation
self.EOF = '\0'
def peek(self):
if self.next_index < len(self.equation):
return self.equation[self.next_index]
else:
return self.EOF
def consume(self):
value = self.peek()
self.next_index = self.next_index + 1
return self.peek()
def expression(self):
coefficient, value = self.term()
while True:
if self.peek() == '+':
self.consume()
coefficient, value = map(add,
(coefficient, value),
self.term())
elif self.peek() == '-':
self.consume()
coefficient, value = map(sub,
(coefficient, value),
self.term())
else:
break
return coefficient, value
def term(self):
sign = 1
if self.peek() == '-':
self.consume()
sign = -1
number = sign * (self.number() if self.peek().isdigit() else 1)
if self.peek() == 'x':
self.consume()
return number, 0
else:
return 0, number
def number(self):
value = 0
while self.peek().isdigit():
value = value * 10 + int(self.peek())
self.consume()
return value
def interpret(self):
result_left = self.expression()
print(result_left)
self.consume()
result_right = self.expression()
print(result_right)
return map(sub, result_left, result_right)
class Solution(object):
def solveEquation(self, equation):
"""
:type equation: str
:rtype: str
"""
interpreter = EquationInterpreter(equation)
coefficient, value = interpreter.interpret()
if coefficient == 0:
if value != 0:
return "No solution"
else:
return "Infinite solutions"
else:
return "x={}".format(-value / coefficient)
Write a recursive descent parser routines for <expr>, <term>, and <factor> given in Section 4.4.1 (see...
Please write a recursive descent parser (including a lexical analyzer) for the following EBNF in C. Your program codes should be runnable. <exprs> -> <expr>; {<expr>;} <expr> -> <term> { (+ | -) <term> } <term> -> <factor> { (*|/ ) <factor> } <factor> -> <exp> {^ <exp>} <exp> -> id | int_lit | real_lit | (<expr>) where, ^ indicates the power operation, id is a legal identifier name, int_lit represents any positive integer number, and real_lit represents any positive...
Question 3: Given the following grammar: assign → id := expr expr → expr + term \ term term -term *factor lfactor factor-(expr) id Using the above grammar, show a leftmost derivation (first five steps) for the following assignment statement: A ((A B)+ C) a. [3 marks] b. Using the above grammar, show a rightmost derivation (first five steps) for the following assignment statement: A:-A+B+C)+A [3 marks] Draw the abstract syntax tree for each of the above statements [4 marks]...
If someone can help me with the recursive descent parsing, it woule be very helpful. This problem should be completed with only c without using lex or yacc. Problem — LL(1) Grammars and Recursive Descent Parsing The Grammar:: <program> ::= program <block> . <block> ::= begin <stmtlist> end <stmtlist> ::= <stmt> <morestmts> <morestmts> ::= ; <stmtlist <stmt> ::= <assign> | <ifstmt> | <whilestmt> | <block> <assign> ::= <variable> = <expr> <ifstmt> ::= if <testexpr> then <stmt> else <stmt> <whilestmt> ::= while <testexpr> do <stmt> <testexpr>...
Write a parser program for cSub using the method of recursive descent. The main program here will effectively be the main program of the compiler as a whole. The input to the program will be a Csub source file, specified on the command line. The program will construct a parse tree for the program, with one interior node for every nonterminal in the derivation of the program, and one leaf node for each of the id, num, and real tokens...
The questions in this section are based on the grammar given as the following: prog -> assign | expr assign -> id = expr expr -> expr + term | expr - term | term term -> factor | factor * term factor -> ( expr ) | id | num id -> A | B | C num -> 0 | 1 | 2 | 3 (2a) What is the associativity of the * operator? (5 points) (2b) What...
The questions in this section are based on the grammar given as the following: prog -> assign | expr assign -> id = expr expr -> expr + term | expr - term | term term -> factor | factor * term factor -> ( expr ) | id | num id -> A | B | C num -> 0 | 1 | 2 | 3 (2a) What is the associativity of the * operator? (5 points) (2b) What...
Name: 3. (10 points) Given grammar: <program> → <stmts> Page: 2 <term> → <var> 1 const 1), write down derivation of: c-5+a 2) What are terminals and what are non-terminals in the grammar? Show a complete parse, including the parse stack contents, input string, and action for the string: id - id + id, using the grammar and parse table below. (10 points) 4. Grammar State id S4 4. T F 5. F (E) R2 S7 R4 R4 R2İR2 Parse...
Write a program in Java, Python and Lisp When the program first launches, there is a menu which allows the user to select one of the following five options: 1.) Add a guest 2.) Add a room 3.) Add a booking 4.) View bookings 5.) Quit The functionality of these options is as follows: 1.) When users add a guest they provide a name which is stored in some manner of array or list. Guests are assigned a unique ID...
1. (p. 2-3.) Which of the following is NOT a reason for studying concepts of programming languages according to Sebesta? a. Increased capacity to express ideas. b. Improved background for choosing appropriate languages. c. Increased ability to design new languages. d. Increased ability to learn new languages. 2. (p. 5-6.) What programming language has dominated scientific computing over the past 50 years? a. FORTRAN b. ALGOL c. SNOBOL d. PL/I 3. (p. 6.) What programming language has dominated artificial intelligence...
making a file You are tasked with creating a text-based program for storing data on Hotel Room Bookings - however, as this is a comparative languages course, you will be creating the same application in the following three programming languages: • Java, • Python, and • Lisp As you implement the application in each language you should keep notes on: - The features of the languages used, - Which features you found useful, and - Any issues or complications which...