Skip to content

Instantly share code, notes, and snippets.

@eliquious
Last active December 31, 2015 07:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eliquious/7953120 to your computer and use it in GitHub Desktop.
Save eliquious/7953120 to your computer and use it in GitHub Desktop.
Expression Grammar which breaks ANTLR v4
grammar expr;
parse
: expression* EOF
;
expression
: assignmentExpression
;
primaryExpression
: trueExpression | falseExpression | nullExpression
| Literal
| integerExpression
| stringExpression
| floatExpression
| qualifiedName
| '(' expression ')'
;
trueExpression
: 'true'
;
falseExpression
: 'false'
;
nullExpression
: 'null'
;
integerExpression
: IntegerLiteral
;
stringExpression
: StringLiteral
;
floatExpression
: FloatingPointLiteral
;
postfixExpression
: primaryExpression
| postfixExpression '[' integerExpression ']'
| postfixExpression '++'
| postfixExpression '--'
;
assignmentExpression
: conditionalExpression (AssignmentOperator conditionalExpression)?
;
conditionalExpression
: logicalOrExpression ('?' expression ':' conditionalExpression)?
;
logicalOrExpression: logicalOAndExpression (LogicalOrOperator expression)*;
logicalOAndExpression: inclusiveOrExpression (LogicalAndOperator expression)*;
inclusiveOrExpression: exclusiveOrExpression (InclusiveOrOperator expression)*;
exclusiveOrExpression: andExpression (ExclusiveOrOperator expression)*;
andExpression: equalityExpression (AndOperator expression)*;
equalityExpression: relationalExpression (EqualityOperator expression)*;
relationalExpression: shiftExpression (RelationalOperator expression)*;
shiftExpression: additiveExpression (ShiftOperator expression)*;
additiveExpression: multiplicativeExpression (AdditiveOperator expression)*;
multiplicativeExpression: castExpression (MultiplicativeOperator expression)*;
castExpression
: unaryExpression ('as' type)?
;
unaryExpression
: postfixExpression
| '!' primaryExpression
| 'sizeof' expression
| expression 'istype' type
;
qualifiedName
: Identifier ('.' Identifier)*
;
type
: (BOOLEAN
| INT8
| UINT8
| INT16
| UINT16
| INT32
| UINT32
| INT64
| UINT64
| FLOAT
| DOUBLE
| CHAR
| STRING)
;
// Lex
BOOLEAN : 'boolean' ;
INT8 : 'int8'; //'byte' ;
UINT8 : 'uint8'; //'ubyte' ;
INT16 : 'int16'; //'short' ;
UINT16 : 'uint16'; //'ushort' ;
INT32 : 'int32'; //'int' ;
UINT32 : 'uint32'; //'uint' ;
INT64 : 'int64'; //'long' ;
UINT64 : 'uint64'; //'ulong' ;
FLOAT : 'float' ;
DOUBLE : 'double' ;
CHAR : 'char';
STRING : 'str';
// Operators
AssignmentOperator
: '=' | '*=' | '/=' | '%=' | '+=' | '-=' | '<<=' | '>>=' | '&=' | '^=' | '|='
;
LogicalOrOperator
: '||'
;
LogicalAndOperator
: '&&'
;
InclusiveOrOperator
: '|'
;
ExclusiveOrOperator
: '^'
;
AndOperator
: '&'
;
EqualityOperator
: '==' | '!='
;
RelationalOperator
: '>' | '<' | '>=' | '<='
;
ShiftOperator
: '<<' | '>>'
;
AdditiveOperator
: '+' | '-'
;
MultiplicativeOperator
: '+' | '/' | '%'
;
// Identifier
Identifier
: Letter LetterOrDigit*
;
fragment
Letter
: (LowercaseLetter | UppercaseLetter)
;
fragment
LetterOrDigit
: [a-zA-Z0-9_]
;
fragment
LowercaseLetter
: [a-z]
;
fragment
UppercaseLetter
: [A-Z]
;
fragment
UppercaseLetterOrUnderscore
: [A-Z_]
;
// Literals
Literal
: IntegerLiteral
| StringLiteral
| FloatingPointLiteral
;
// §3.10.5 String Literals
StringLiteral
: '"' StringCharacters? '"'
;
fragment
StringCharacters
: StringCharacter+
;
fragment
StringCharacter
: ~["\\]
| EscapeSequence
;
// §3.10.6 Escape Sequences for Character and String Literals
fragment
EscapeSequence
: '\\' [btnfr"'\\]
;
// §3.10.1 Integer Literals
IntegerLiteral
: DecimalIntegerLiteral
| HexIntegerLiteral
| BinaryIntegerLiteral
;
fragment
DecimalIntegerLiteral
: DecimalNumeral
;
fragment
HexIntegerLiteral
: HexNumeral
;
fragment
BinaryIntegerLiteral
: BinaryNumeral
;
fragment
DecimalNumeral
: '0'
| NonZeroDigit Digits*
;
fragment
Digits
: Digit+
;
fragment
Digit
: '0'
| NonZeroDigit
;
fragment
NonZeroDigit
: [1-9]
;
fragment
HexNumeral
: '0' [xX] HexDigits
;
fragment
HexDigits
: HexDigit+
;
fragment
HexDigit
: [0-9a-fA-F]
;
fragment
BinaryNumeral
: '0' [bB] BinaryDigits
;
fragment
BinaryDigits
: BinaryDigit+
;
fragment
BinaryDigit
: [01]
;
// §3.10.2 Floating-Point Literals
FloatingPointLiteral
: DecimalFloatingPointLiteral
;
fragment
DecimalFloatingPointLiteral
: Digits '.' Digits? ExponentPart?
| '.' Digits ExponentPart?
| Digits ExponentPart
| Digits
;
fragment
ExponentPart
: ExponentIndicator SignedInteger
;
fragment
ExponentIndicator
: [eE]
;
fragment
SignedInteger
: Sign? Digits
;
fragment
Sign
: [+-]
;
WS : [ \t\r\n] -> skip;
LINE_COMMENT
: '//' ~[\r\n]* -> skip
;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment