Created
April 21, 2013 14:21
-
-
Save tangentstorm/5429758 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
grammar CorePascal; | |
parse : module ; | |
//--- parser ------------------------------------- | |
module | |
: modHeader | |
block '.' | |
; | |
modHeader | |
: 'module' IDEN ';'? | |
; | |
block | |
: 'begin' ( statement ';' )* 'end' | |
; | |
statement | |
: // pass | |
; | |
//--- scanner ------------------------------------ | |
WHITESPACE | |
: ('\u0000' .. '\u0020' )+ // space + all ascii control chars | |
-> channel(HIDDEN) | |
; | |
IDEN | |
: Alpha ( Alpha | Digit | '_' )* | |
; | |
fragment Alpha : 'a'..'z' | 'A'..'Z' ; | |
fragment Digit : '0'..'9' ; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module hello; | |
begin | |
end. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# export CLASSPATH=".:/path/to/antlr-4.0-complete.jar:$CLASSPATH" | |
# alias grun='java org.antlr.v4.runtime.misc.TestRig' | |
$ grun CorePascal parse -trace hellocore.pas | |
enter parse, LT(1)=module | |
enter module, LT(1)=module | |
enter modHeader, LT(1)=module | |
consume [@0,0:5='module',<3>,1:0] rule modHeader alt=1 | |
consume [@2,7:15='hellocore',<7>,1:7] rule modHeader alt=1 | |
consume [@3,16:16=';',<4>,1:16] rule modHeader alt=1 | |
exit modHeader, LT(1)=begin | |
enter block, LT(1)=begin | |
consume [@5,18:22='begin',<2>,2:0] rule block alt=1 | |
consume [@7,24:26='end',<5>,3:0] rule block alt=1 | |
exit block, LT(1)=. | |
consume [@8,27:27='.',<1>,3:3] rule module alt=1 | |
exit module, LT(1)=<EOF> | |
exit parse, LT(1)=<EOF> | |
$ grun CorePascal parse -tree hellocore.pas | |
(parse (module (modHeader module hellocore ;) (block begin end) .)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment