Consider the following grammar 0 1 2 3 4 5 6 7

Consider the following grammar:

<expr> ::= <num> | <expr> + <num> | <expr> - <num>

<num> ::= 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9

Is the grammar ambiguous? Is the grammar regular? Is the grammar context free?

Solution

Answer:

Yes this grammar is ambiguous . Let us see this production

<expr> :: = <expr> + <num>

In this we can from two parse tree. Either we go with <expr> or <num> , we have two choices that means two parse trees. Hence ambigious.

---> Yes this is regular. Result could be any number between 1 to 9 with + or - arthmetic. We can calculate its value thus regular.

---> Yes it is a context free grammar :

It has terminals , non terminals , productions and symbols in the given grammar.

Consider the following grammar: <expr> ::= <num> | <expr> + <num> | <expr> - <num> <num> ::= 0 | 1 | 2 | 3 | 4 | 5 | 6

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site