PROGRAMMING LANGUAGES Give an EBNF grammar for each of the languages of Exercise
ID: 3797709 • Letter: P
Question
PROGRAMMING LANGUAGES
Give an EBNF grammar for each of the languages of Exercise 1. Use the EBNF extensions wherever possible to simplify the grammars. Include whatever notes to the reader are required to make the associativity of the operators clear.
Below is Exercise #1
Start with grammar G6
<exp> ::= <exp> + <mulexp> | <mulexp>
<mulexp> ::= <mulexp> * <rootexp> | <rootexp>
<rootexp> ::= (<exp>) | a | b | c
a. Add subtraction and division (- and /) to G6 with the customary precedence and associativity.
b. Add a left-associative operator % to G6 between the + and * in precedence.
c. Add a right-associative = operator to G6, at lower precedence than + or *.
Explanation / Answer
a)Since '-' has the same precedence and associativity as '+' and '/' has the same precedence and associativity as '*',
<exp>::=<exp>[+|-]<mulexp>|<mulexp>
<mulexp>::=<mulexp>[*|/]<rootexp>|<rootexp>
<rootexp>::=(<exp>)|a|b|c
b)To add the '%' operator we introduce a non-terminal <percexp> which has precedence bewtween '+' and '*' (Note- I have left the amendments made for question a) as they are. You may remove them if they are not required)
<exp>::=<exp>[+|-]<percexp>|<percexp>
<percexp>::=<mulexp>%<rootexp>|<rootexp>|<mulexp>
<mulexp>::=<mulexp>[*|/]<rootexp>|<rootexp>
<rootexp>::=(<exp>)|a|b|c
c) To handle the '=' operator with the lowest precedence, we introduce a non-terminal <eqexp>. Again you may remove amendments made for questions a) and b) from below if they are not required.
<eqexp>::=<exp>=<percexp>|<mulexp>|<rootexp>|<exp>
<exp>::=<exp>[+|-]<percexp>|<percexp>
<percexp>::=<mulexp>%<rootexp>|<rootexp>|<mulexp>
<mulexp>::=<mulexp>[*|/]<rootexp>|<rootexp>
<rootexp>::=(<exp>)|a|b|c
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.