[antlr-interest] recognizing a function
Guy Kroizman
kroizguy at gmail.com
Thu Jul 24 12:35:53 PDT 2008
Howdy,
I have written a grammar that I hoped would find a function definition in a
Fortran file.
Running it produces nothing. s-:
I played with it a lot and debugged it with jdb and ANTLRWorks but to avail.
I wonder if anybody would be so kind to point me to the problem with the
grammar.
grammar fun;
>
> root :
> (functionStatement)*
> ;
>
> functionStatement :
> (type)? FUNCTION NAME '(' (namelist)? ')'
> {
> System.out.println( "func: " + $NAME.text );
> };
>
>
> namelist:
> identifier ( ',' identifier )* ;
>
> identifier :
> NAME
> ;
>
> type :
> (
> ('r'|'R')('e'|'E')('a'|'A')('l'|'L') |
> ('c'|'C')('o'|'O')('m'|'M')('p'|'P')('l'|'L')('e'|'E')('x'|'X')|
> ('D'|'d')('O'|'o')('U'|'u')('B'|'b')('L'|'l')('E'|'e')
> ('C'|'c')('O'|'o')('M'|'m')('P'|'p')('L'|'l')('E'|'e')('X'|'x') |
> ('D'|'d')('O'|'o')('U'|'u')('B'|'b')('L'|'l')('E'|'e')
> ('P'|'p')('R'|'r')('E'|'e')('C'|'c')('I'|'i')('S'|'s')('I'|'i')('O'|'o')('N'|'n')
> |
> ('i'|'I')('N'|'n')('T'|'t')('E'|'e')('G'|'g')('E'|'e')('R'|'r') |
> ('L'|'l')('O'|'o')('G'|'g')('I'|'i')('C'|'c')('A'|'a')('L'|'l')
> )
> ;
>
> FUNCTION:
> ('f'|'F')('u'|'U')('n'|'N')('c'|'C')('t'|'T')('i'|'I')('o'|'O')('n'|'N');
>
> WHITE: (' ' | '\t' | '\n' | '\r')+ {skip();} ;
>
> // identifier (keyword or variable)
> NAME : ('a'..'z'|'A'..'Z') ('a'..'z'|'A'..'Z'|'0'..'9')*
> ;
>
> ANY : . ;
>
I have tested it with the following input:
PROGRAM TSTFAC
>
> INTEGER N
> REAL FAC
>
> 10 WRITE(*,*) 'ENTER THE VALUE N (CNTRL-C TO END):'
> READ(*,*) N
>
> IF (N .GE. 0) THEN
> WRITE(*,*) N, '! = ',FAC(N)
> GOTO 10
> ELSE
> WRITE(*,*) 'INVALID VALUE FOR N.'
> GOTO 10
> ENDIF
>
> STOP
> END
>
> REAL FUNCTION FAC(N)
> INTEGER N
> N = 7
> RETURN
> END
>
with the following test rig:
import org.antlr.runtime.*;
> import java.io.*;
>
> public class Test {
> public static void main(String[] args) throws Exception {
> try{
> FileInputStream f = new FileInputStream( "tstfac.f" );
> ANTLRInputStream input = new ANTLRInputStream( f );
> funLexer lexer = new funLexer(input);
> CommonTokenStream tokens = new CommonTokenStream(lexer);
> funParser parser = new funParser(tokens);
> parser.root();
> }
> catch(FileNotFoundException e)
> {
> System.out.println("file not found");
> }
>
> }
> }
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20080724/adece0bd/attachment.html
More information about the antlr-interest
mailing list