Skip to content
Advertisement

How can I make this grammar unambiguous?

I’m trying to write a parser for a simple language:

JavaScript

When I run it, I get the following error:

JavaScript

I understand that this is happening because, if we imagine that the parser was built without an error, and then wrote parser.parse('foo'), both arithmetic_expr and boolean_expr would be “correct” derivations. Also, as you can see I’m using LALR which is a strictly deterministic algorithm and can’t handle ambiguities.

So my question is, how can I make this grammar unambiguous? I can’t seem to find the solution.

Advertisement

Answer

You can’t and you shouldn’t.

Don’t try to use a grammar to do type-checking. Types are semantic, not syntactic. The lexicon can’t tell you whether an ID is boolean or arithmetic (unless you enforce Hungarian naming), so the grammar can only tell “sometimes”. And sometimes isn’t good enough.

But it doesn’t matter. You can easily do the type analysis during the semantic passes after you’ve built the syntax tree. Until then, an expression is an expression.

What I’d do is get rid of bool_atom. Just use a complete expression hierarchy with (boolean) expression at the top and atom at the bottom, placing rel_op where it would naturally go (in bool_term, instead of bool_atom). However, that does change the grammar in one way. In the existing grammar, the expression

JavaScript

means !(a < b). That may be what you expected, and if so you can accommodate it with a bit of work, but it’s a bit different from the semantics of most languages I know. My suggested grammar would require the use of parentheses in that case.

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement