[antlr-interest] recognizing a function

Ana Nelson nelson.ana at gmail.com
Thu Jul 24 14:51:33 PDT 2008


Guy,

I have looked at your grammar and have a few suggestions, but in general I
think you are making a very common mistake (which I myself have made many
times) which is to try to write a complete grammar from scratch. I've been
using ANTLR for about a year now, and I have learned (the hard way) to start
my grammars very simply with just 1 or 2 rules and build them up from there,
with the help of a good testing framework (such as gUnit). In this case, I
would first write a grammar which just matches "FUNCTION" then extend it to
match "FUNCTION FAC" then "REAL FUNCTION FAC" then "REAL FUNCTION FAC(N)"
etc. It's just so easy and painless to build grammars up from very small
pieces which you understand and so frustrating and painful to do it the
other way around. :-)

Now, to get your grammar working, try these 2 things:
1) change your input text file to JUST have the text REAL FUNCTION FAC(N)
2) change your rule "type" into a token "TYPE"

If you do this, your code should work as you expect.

However, the main problem with your grammar is that it's not able to parse
anything that doesn't look like REAL FUNCTION FAC(N), so if you try to parse
your full example text, it's never going to get past "PROGRAM TSTFAC" since
this doesn't match the functionStatement rule.

If you are only interested in fetching function names and want to ignore
everything else, then you probably want something like...

root
  : statement+
  ;

statement
  : functionStatement NEWLINE
  | otherStatement NEWLINE
  ;

I'm implying here that you should have a separate NEWLINE token, and take
newline characters out of your WHITE token since you probably want a
line-based parser (for now anyway). Also you'll have to figure out what
should go in otherStatement (or whatever you want to call it)

Hope this makes sense. There is a full FORTRAN grammar on the ANTLR website
which you might also want to look at.

Regards,
Ana Nelson
--
http://ananelson.com



2008/7/24 Guy Kroizman <kroizguy at gmail.com>:

> 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/18ff7df0/attachment-0001.html 


More information about the antlr-interest mailing list