1

I need to construct a parser for an expression which is allowed to have only 2 operators (AND & OR) and such that both operators cannot be presenet in a enclosing paranthesis.

Eg1:

(A AND B OR C) --> invalid
(A AND (B OR C)) --> valid

Eg2:

(A AND (B OR C) OR D) --> invalid
((A AND (B OR C)) OR D) --> valid
(A AND ((B OR C) OR D)) --> valid

Please note: I'm very new to this. I had tried to attempt this using LL(1) parser by following a few online tutorials. Here is what I've achieved so far.

E -> F E'
E' -> OR F E'
E' -> ''
F -> G F'
F' -> AND G F'
F' -> ''
G -> ( E )
G -> id

The above grammar rules checks whether I have only the valid operators & parenthesis but doesn't not identify the ambiguity problem. I'm not expecting a solution here but I just wanna know if I'm on the right track. Because in one of the tutorial the tutor had mentioned that LL(1) parser will not work if there is more than one lookup for a terminal symbol. I know that I haven't encountered such a situation yet but will I (particularly when generating the rules for resolving the ambiguity)???

Edit1

E -> F E'
E' -> OR F E'
E' -> AND F E'
E' -> ''
F -> ( E )
F -> id

Edit2: Solution

E -> F E'
E' -> and F G
E' -> or F H
E' -> ''
G -> and F G
G -> ''
H -> or F H
H -> ''
F -> ( E )
F -> id
DhiwaTdG
  • 748
  • 1
  • 10
  • 26
  • 1
    Oops, sorry. Missed an empty production. Anyway, the thing to think about: what goes inside parentheses? It is an expression optionally followed either by a series of ANDs or by a series of ORs. You already have the productions for both types of series, so you just have to put them together correctly. – rici Feb 08 '17 at 16:12
  • Exactly, I had the same idea too and I've updated the question with my new rules. But still I'm missing somewhere... – DhiwaTdG Feb 08 '17 at 16:30
  • 1
    Hint: the `E'` at the end of `E' -> OR F E'` allows for `x OR y OR z`. – BeyelerStudios Feb 08 '17 at 16:48
  • Another hint: G and H can't end with the same non-terminal, because they don't have the same possible continuations. – rici Feb 08 '17 at 18:49
  • @rici How about now? – DhiwaTdG Feb 12 '17 at 19:55
  • That looks right to me at a quick glance. – rici Feb 12 '17 at 20:18
  • Thanks a ton!!! :-) – DhiwaTdG Feb 12 '17 at 20:21

0 Answers0