Related articles |
---|
Parsing an If...End If construct mness1978@hotmail.com (2004-11-17) |
Re: Parsing an If...End If construct snicol@apk.net (Scott Nicol) (2004-11-19) |
Re: Parsing an If...End If construct vbdis@aol.com (2004-11-19) |
Re: Parsing an If...End If construct cfc@shell01.TheWorld.com (Chris F Clark) (2004-11-20) |
From: | Scott Nicol <snicol@apk.net> |
Newsgroups: | comp.compilers |
Date: | 19 Nov 2004 00:49:50 -0500 |
Organization: | Compilers Central |
References: | 04-11-047 |
Keywords: | parse, syntax |
Posted-Date: | 19 Nov 2004 00:49:50 EST |
Michael wrote:
> The bison grammar I have so far is listed below. It is has several
> conflicts that I'm having trouble resolving. Can anyone give me some
> pointers on how to deconflict this grammar?
>[...]
> instruction : END
> | GOSUB destination
> | GOTO destination
> | IF conditions THEN statements opt_else
> | IF conditions statements else_if_list opt_else END IF
> | PRINT print_list
> | RETURN
> ;
>
> else_if_list : else_if_list else_if
> | else_if
> ;
>
> else_if : ELSE IF conditions statements
> | /* empty */
> ;
>
> opt_else : ELSE statements
> | /* empty */
> ;
A few obvious problems. The END IF and ELSE IF are ambiguous beyond 1
token of lookahead. You can fix this by disambiguating in the
scanner, producing ELSEIF and ENDIF tokens. Second problem is that
you are burying the optional else_if_list too deep.
Rewrite your grammar like this:
instruction: END
| GOSUB destination
| GOTO destination
| IF conditions THEN statements opt_else
| IF conditions THEN statements opt_else_if_list opt_else ENDIF
| PRINT print_list
| RETURN
;
opt_else_if_list: /* empty */ | else_if_list;
else_if_list: else_if
| else_if_list else if
;
else_if: ELSEIF conditions statement;
opt_else: /* empty */ | ELSE statements;
You will have 2 shift/reduce conflicts left:
1. the classic if/then/else conflict
2. the BASIC classic colon conflict
both of which you want to shift so it is OK if you leave them alone.
--
Scott Nicol
snicol@apk.net
Return to the
comp.compilers page.
Search the
comp.compilers archives again.