I wrote an Antlr grammar for parsing functions from C source code files:
grammar newCfunctions;
options
{
language = CSharp;
}
@parser::namespace { Generated }
@lexer::namespace { Generated }
func
:function+ { Console.WriteLine("hello"); } //this is for debugging
;
NAME
:[a-zA-Z]+[a-zA-Z0-9]*
;
TYPENAME
: 'void'
| [a-zA-Z]+
| 'char'
| 'short'
| 'int'
| 'long'
| 'float'
| 'double'
| 'signed'
| 'unsigned'
| '_Bool'
| '_Complex'
| '__m128'
| '__m128d'
| '__m128i'
| NAME
;
arguments
: (TYPENAME NAME)*
;
Newline
: '\r'? '\n' ;
FUNCTIONBODY
: ([a-zA-Z0-9]|Newline)*;
function
: TYPENAME ' ' NAME '(' arguments ')' ' '? Newline? '{' FUNCTIONBODY '}' Newline?
;
I generatet C# files and included them into test project. Main function of it:
try
{
AntlrInputStream input = new AntlrInputStream(Console.In);
newCfunctionsLexer lexer = new newCfunctionsLexer(input);
CommonTokenStream tokens = new CommonTokenStream(lexer);
newCfunctionsParser parser = new newCfunctionsParser(tokens);
parser.func();
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
Console.ReadKey();
When I write "void foo(int a){return a;}" it gives me ann error: "line 1:0 mismatched input 'void' expecting TYPENAME". Please, help me with this grammar! I saw C grammar in the Internet, but it has 800+ lines and i don't know what to do with it. If you know, how to use it, promt me please. Thank you!