Skip to content

Instantly share code, notes, and snippets.

@mdornseif
Created March 11, 2010 07:53
Show Gist options
  • Save mdornseif/328943 to your computer and use it in GitHub Desktop.
Save mdornseif/328943 to your computer and use it in GitHub Desktop.
// This is a ANTLR3 Grammar for slidescript
grammar Slidescript;
options {
language=Python;
output=AST;
}
tokens { ASSIGNMENT;
CODE; }
/* This will be the entry point of our parser. */
program: (code);
//section: (code); // | text);
code: ( stat )+ -> ^(CODE stat+) ;
stat: VARIABLE '=' additionExp (NEWLINE|EOF) -> ^(ASSIGNMENT VARIABLE additionExp)
| NEWLINE ->
;
//assignment: VARIABLE '=' additionExp -> ^(ASSIGNMENT VARIABLE additionExp);
/* Addition and subtraction have the lowest precedence. */
additionExp
: multiplyExp ((PLUS^|MINUS^) multiplyExp)*
;
/* Multiplication and addition have a higher precedence. */
multiplyExp
: atomExp ((MAL^|DURCH^) atomExp)*
;
/* An expression atom is the smallest part of an expression: a number. Or
when we encounter parenthesis, we're making a recursive call back to the
rule 'additionExp'. As you can see, an 'atomExp' has the highest precedence. */
atomExp
: CONSTANT
| VARIABLE
| '('! additionExp ')'!
;
text: NEWLINE (options {greedy=false;} : .)* NEWLINE ;
/* A number: can be an integer value, or a decimal value */
CONSTANT: ('0'..'9')+ ('.' ('0'..'9')+)? ;
VARIABLE: ('A'..'Z') ('a'..'z'|'A'..'Z'|'0'..'9'|'_')+ ;
PLUS: '+';
MINUS: '-';
MAL: '*';
DURCH: '/';
ML_COMMENT
: '/*' (options {greedy=false;} : .)* '*/' {$channel=HIDDEN;}
;
LINE_COMMENT
: '//' ~('\n'|'\r')* '\r'? '\n' {$channel=HIDDEN;}
;
NEWLINE: '\r'? '\n';
/* We're going to ignore all white space characters */
WHITESPACE : ( '\t' | ' ' | '\u000C' | NEWLINE )+ { $channel = HIDDEN; };
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment