Skip to content

Instantly share code, notes, and snippets.

@chenzx
Created December 25, 2013 09:57
Show Gist options
  • Save chenzx/8121847 to your computer and use it in GitHub Desktop.
Save chenzx/8121847 to your computer and use it in GitHub Desktop.
Antlr4 grammar file to parser C-style "typedef struct { ... } name;" definitions
grammar UserType;
@header {
package test;
}
@lexer::members{
public static final int WHITESPACE = 1;
public static final int COMMENTS = 2;
}
userStructTypeDefs : userStructTypeDef+
;
userStructTypeDef : 'typedef' 'struct' '{' structFieldDecl+ '}' NAME ';'
;
structFieldDecl : primitiveType NAME ';' # PrimitiveFieldDecl
| arrayElementType NAME '[' dim1 ']' ';' # Array1DFieldDecl
| arrayElementType NAME '[' dim1 ']' '[' dim2 ']' ';' # Array2DFieldDecl
| userType NAME ';' # UserTypeFieldDecl
;
arrayElementType : primitiveType #PrimitiveArrayElementType
| userType #UserArrayElementType
;
dim1 : NUMBER ;
dim2 : NUMBER ;
primitiveType : 'int'
| 'double'
| 'char'
| 'CHAR'
| 'BOOL'
| 'DWORD'
;
userType : NAME ;
WS : [ \t\r\n]+ -> channel(WHITESPACE);
COMMENT : '//' ~[\n]+ -> channel(COMMENTS);
NUMBER : [0-9]+ ;
NAME : [a-z_A-Z][a-z_A-Z0-9]* ;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment