Parsing an If...End If construct

mness1978@hotmail.com (Michael)
17 Nov 2004 11:27:53 -0500

          From comp.compilers

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)
| List of all articles for this month |
From: mness1978@hotmail.com (Michael)
Newsgroups: comp.compilers
Date: 17 Nov 2004 11:27:53 -0500
Organization: http://groups.google.com
Keywords: parse, question
Posted-Date: 17 Nov 2004 11:27:53 EST

I'm attempting to create a compiler for an old flavor of BASIC called
AMOS. The grammar of this language uses the IF...THEN...ELSE
construct but also specifies an alternative IF construct called
IF...END IF.


There are 4 forms:


IF condition statements END IF
IF condition statements ELSE statements END IF
IF condition statements ELSE IF statements ... END IF
IF condition statements ELSE IF statements ... ELSE statements END IF


So that something like could be written:


If X=10
      Print "Ten"
Else If X=20
      Print "Twenty"
Else If X=30
      Print "Thirty"
Else
      End
End If


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?


Thanks,
Mike






%token NEWLINE LABEL LINE_NUMBER COLON
%token END GOSUB GOTO PRINT RETURN
%token IF THEN ELSE


%nonassoc ELSE
%nonassoc COLON




program : lines
| /* empty */
;


lines : lines line
| line
;


line : label statements NEWLINE
;


label : LABEL
| LINE_NUMBER
| /* empty */
;


destination : LABEL
| LINE_NUMBER
;


statements : statements COLON statement
| statement
| /* empty */
;


statement : instruction
;


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 */
;


Post a followup to this message

Return to the comp.compilers page.
Search the comp.compilers archives again.