Re: BNF for a BASIC compiler

"antoine" <the-xogos@ifrance.com>
13 Oct 2002 16:41:28 -0400

          From comp.compilers

Related articles
BNF for a BASIC compiler mness1978@hotmail.com (Michael Ness) (2002-09-29)
Re: BNF for a BASIC compiler snicol@apk.net (Scott Nicol) (2002-10-13)
Re: BNF for a BASIC compiler michael@moria.de (Michael Haardt) (2002-10-13)
Re: BNF for a BASIC compiler the-xogos@ifrance.com (antoine) (2002-10-13)
| List of all articles for this month |

From: "antoine" <the-xogos@ifrance.com>
Newsgroups: comp.compilers
Date: 13 Oct 2002 16:41:28 -0400
Organization: None
References: 02-09-171
Keywords: Basic, parse
Posted-Date: 13 Oct 2002 16:41:28 EDT

On 29 Sep 2002 16:59:34 -0400, "Michael Ness" <mness1978@hotmail.com>
wrote:


>Here is my first attempt at a defining a grammer using BNF. Is this the
>way BNF should look? Can anyone point out any mistakes I'm making? One
>thing that I am concerned about is the two token statment "END IF". Is


I'm not a LALR(1) specialist, but you shoud have a look at the
Yacc/Bison documentation. You should also look at the ADA95 grammar,
as there is such a construction:


http://adam.wins.uva.nl/~x/grammars/ada/#use:if_statement




>this legal in a LALR(1) parser? What about the <identifier> =
><expression> assignment with a single "="? Is it possible to not
>confuse this with the <expression> = <expression> boolean test?


if you mean to use the same operator for assignment and for equality,
I think this is very error-prone, and leads to hard-to-find execution
errors (see the x=y (true if y <> 0) and x==y (true if x equals y) of
the C programming language..)


It is feasable but you'll have to check this during the semantic
analysis of the program. But how will you assign a value to boolean
variables ? You'll only have a subset of the expressions you usually
have in a P.L...


>
>------------------------------------------------------------------------
>
><program> ::= <lines>
>
><lines> ::= <line><lines> |
> <line>
>
><line> ::= NUM <statements> CR |
> LABEL <statements> CR |
> <statements> CR |
> CR
>
><statements> ::= <statement> COLON <statements> |
> <statement>
>
><statement> ::= IF <boolean> GOTO <label> [ ELSE <statements> ] |
> IF <boolean> THEN <statements> [ ELSE <statements> ] |
> IF <boolean> <statements> [ ELSE <statements> ] ENDIF |
> FOR <identifier> = <expression> TO <expression> [ STEP <expression> ] |
> WHILE <boolean> <statements> WEND |
> REPEAT <statements> UNTIL <boolean> |
> DO <statements> LOOP |
> EXIT IF <expression> |
> INPUT <identifier> |
> PRINT <expression> |
> ? <expression> |
> GOTO <label> |
> LET <identifier> = <expression> |
> <identifier> = <expression> |
> END |
> <empty>


Here is how is it done in ADA95:
http://www.adaic.org/standards/95lrm/grammar9x.y


if_stmt : IF cond_clause_s else_opt END IF ';'
;


cond_clause_s : cond_clause
| cond_clause_s ELSIF cond_clause
;


cond_clause : cond_part statement_s
;


cond_part : condition THEN
;


condition : expression
;


else_opt :
| ELSE statement_s
;
.. and so on.


><empty> ::= NULL
>
><label> ::= <expression> |
> <identifier>
>
><expression> ::= <term> + <expression> |
> <term> - <expression>
>
><term> ::= <factor> * <term> |
> <factor> / <term>
>
><factor> ::= ( <expression ) |
> <identifier> |
> <number>
>
><identifier> ::= <letter> { <letter> | <number> } |
>
><letter> ::= A-F | a-f | "_"
><number> ::= 0-9
>
><boolean> ::= <expression> > 0 |
> <expression> = <expression> |
> NOT <identifier> = <identifier> |
> <expression> > <expression> |
> <expression> < <expression> |
> <expression> >= <expression> |
> <expression> <= <expression>
>
>------------------------------------------------------------------------
You may also look at this online book:
http://caml.inria.fr/oreilly-book/html/book-ora058.html#toc79




It's a BASIC interpreter written in Objective Caml.


HTH
--
(antoine)


Le repas n'est pas prêt: (A)bandonner (R)éessayer ou (P)izza ?


Post a followup to this message

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