I was trying the first V4 example from the "Getting started page".
http://www.antlr.org/wiki/display/ANTLR4/Getting+Started+with+ANTLR+v4
Java files were generated and compiled but I have no success, because obtained "Can't load Hello as lexer or parser" while calling TestRig.
http://screencast.com/t/LbFc7BcZU
C:\programming\Java\libraries\ANTLR 4.0>antlr4.bat Hello.g4
C:\programming\Java\libraries\ANTLR 4.0>java -cp antlr-4.0-complete.jar org.antlr.v4.Tool Hello.g4
C:\programming\Java\libraries\ANTLR 4.0>javac -cp antlr-4.0-complete.jar Hello*.java
C:\programming\Java\libraries\ANTLR 4.0>run Hello r -tree
C:\programming\Java\libraries\ANTLR 4.0>java -cp antlr-4.0-complete.jar org.antlr.v4.runtime.misc.TestRig Hello r -tree
Can't load Hello as lexer or parser
C:\programming\Java\libraries\ANTLR 4.0>dir
...
23.01.2013 00:28 <DIR> .
23.01.2013 00:28 <DIR> ..
22.01.2013 21:37 1Â 216Â 230 antlr-4.0-complete.jar
22.01.2013 21:38 554Â 786 antlr-4.0.tar.gz
22.01.2013 21:38 242Â 694 antlr-runtime-4.0.jar
23.01.2013 00:18 <DIR> antlr4-4.0
23.01.2013 00:20 52 antlr4.bat
23.01.2013 00:25 235 Hello.g4
23.01.2013 00:31 31 Hello.tokens
23.01.2013 00:31 794 HelloBaseListener.class
23.01.2013 00:31 656 HelloBaseListener.java
23.01.2013 00:31 2Â 512 HelloLexer.class
23.01.2013 00:31 2Â 471 HelloLexer.java
23.01.2013 00:31 31 HelloLexer.tokens
23.01.2013 00:31 304 HelloListener.class
23.01.2013 00:31 258 HelloListener.java
23.01.2013 00:31 869 HelloParser$RContext.class
23.01.2013 00:31 3Â 026 HelloParser.class
23.01.2013 00:31 2Â 620 HelloParser.java
23.01.2013 00:24 68 run.bat
...
The content of grammar file I've just copy-pasted from web page.
Ooops. Sorry about that. Can you try with dot in your CLASSPATH like
java -cp .;antlr-4.0-complete.jar org.antlr.v4.runtime.misc.TestRig Hello r -tree
? please let me know that words and I will update the homepage.
Yes, it was exactly classpath issue. Thank you, and sorry for my panic :)
now it works better. not ideal - because reports error but better.
C:\programming\Java\libraries\ANTLR 4.0>java -cp .;antlr-4.0-complete.jar org.antlr.v4.runtime.misc.TestRig Hello r -tree
hello parrt
^Z
line 1:11 token recognition error at: '\r'
(r hello parrt)
C:\programming\Java\libraries\ANTLR 4.0>
The WS
rule of the grammar needed \r
added to it. I updated the Getting Started page to reflect.
yes, r helps and now sample works nice. Thank you :)
I seem to get the same error as you. I run antlr4 command on Hello file, the Hello*.java files are created, I compile them, and when i run grun I get the message Can't load Hello as lexer or parser. Any suggestions on how to fix this ? Hello.g4 has the grammar available on antlr website.
Did you edit the Hello.g4 file as mentioned in the comments above?
Hi
I'm facing same issue. followed the instructions but still need good advice
D:ANTLR>echo %CLASSPATH%
D:ANTLRantlr-4.0-complete.jar;.
D:ANTLR>java -cp D:ANTLRantlr-4.0-complete.jar;. org.antlr.v4.runtime.misc.TestRig Hello r -tree -gui -trace
Can't load Hello as lexer or parser
Directory of D:ANTLR
25/09/13 12:23 101 hello.g4
25/09/13 12:30 31 Hello.tokens
25/09/13 12:30 1,436 HelloBaseListener.java
25/09/13 12:30 2,662 HelloLexer.java
25/09/13 12:30 31 HelloLexer.tokens
25/09/13 12:30 613 HelloListener.java
25/09/13 12:30 2,801 HelloParser.java
7 File(s) 7,675 bytes
Hello.g4
grammar Hello;
r : 'hello' ID ;
ID : [a-z]+ ;
WS : [ trn]+ -> skip ;
Thanks for any help !!
feel free to compile the source :)
Hi,
Not sure I follow
The grammar file was copied and has the r .. yet it does not work for me
Thanks
Ben
From: Terence Parr [mailto:[email protected]]
Sent: Wednesday, September 25, 2013 18:20
To: antlr/antlr4
Cc: BR99
Subject: Re: [antlr4] Sample doesn't work on Windows environment. (#147)
feel free to compile the source :)
—
Reply to this email directly or view it on GitHub https://github.com/antlr/antlr4/issues/147#issuecomment-25096009 . https://github.com/notifications/beacon/6kp6OnXcvE3U-_DAG9_vmagTY8ENz-uEYGdPXhGbmqwt-GtF0ueJAoHfH738TshD.gif
hi. javac compiles .java to .class which is what actually is executed by the VM. TestRig is not a compiler and so cannot run .java files.
Thanks a lot
Ben
From: Terence Parr [mailto:[email protected]]
Sent: Wednesday, September 25, 2013 20:18
To: antlr/antlr4
Cc: BR99
Subject: Re: [antlr4] Sample doesn't work on Windows environment. (#147)
hi. javac compiles .java to .class which is what actually is executed by the VM. TestRig is not a compiler and so cannot run .java files.
—
Reply to this email directly or view it on GitHub https://github.com/antlr/antlr4/issues/147#issuecomment-25106672 . https://github.com/notifications/beacon/6kp6OnXcvE3U-_DAG9_vmagTY8ENz-uEYGdPXhGbmqwt-GtF0ueJAoHfH738TshD.gif
Oh... I think I have to punch myface for not having compiled the classes. I was much concerned about the grammar and all stuff that my eyes missed the "javac _.java" at page 6 of the book... (_facepalm*).
@parrt: Many thanks for the answer
hahahah. no problem. Good luck!
I'm having a similar problem and can't figure out what's wrong. Help anyone?
dot in classpath. is that missing in the getting started?
Of course. That was the problem. I thought it was hanging and didn't realize it was waiting for input. Thank you.
I am able to compile the grammar. However, when I issue the "grun" command on the CLI, it just hangs, no output, as if it is going into infinite loop. Any ideas? Compiled/running using Java(TM) SE Runtime Environment (build 1.7.0_60-b19).
i have also similar issue.......
Grun just struck at one point and not showing anything
try EOF (ctrl-D or ctrl-Z) on line by itself if reading from stdin. You will find the FAQ oh so useful folks. https://theantlrguy.atlassian.net/wiki/pages/viewpage.action?pageId=7143433
I am using the c grammar written by you. I have just added following lines in the grammar.
program : entryPoint* EOF;
entryPoint : primaryExpression
| statement
| declaration
| compilationUnit
;
Then I tried to test a small c program using the command
grun C program -tree test.c
and my system was hanged.
P.S. the grammar is working when compilationUnit is not the part of entry point and producing output as well as.
see faq link
I have seen it. It's not EOF problem. grun C program -tree test.c is working fine when i used
program : entryPoint* EOF;
entryPoint : primaryExpression
| statement
| declaration;
As I added compilationUnit the program was not responding.
This is the code. I do not do what's wrong with it.
/*
[The "BSD licence"]
Copyright (c) 2013 Sam Harwell
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/** C 2011 grammar built from the C11 Spec */
grammar cpp;
program : entryPoint* EOF;
entryPoint : primaryExpression
| statement
| declaration
| compilationUnit
;
primaryExpression
: IDENTIFIER
| CONSTANT
| STRINGLITERAL+
| LEFTPAREN expression RIGHTPAREN
| genericSelection
| EXTENSION? LEFTPAREN compoundStatement RIGHTPAREN // Blocks (GCC
extension)
| BUILTINVAARG LEFTPAREN unaryExpression COMMA typeName RIGHTPAREN
| BUILTINOFFSETOF LEFTPAREN typeName COMMA unaryExpression RIGHTPAREN
;
genericSelection
: GENERIC? LEFTPAREN asSIGNmentExpression COMMA genericAssocList
RIGHTPAREN
;
genericAssocList
: genericAssociation
| genericAssocList COMMA genericAssociation
;
genericAssociation
: typeName COLON asSIGNmentExpression
| DEFAULT COLON asSIGNmentExpression
;
postfixExpression
: primaryExpression
| postfixExpression LEFTBRACKET expression RIGHTBRACKET
| postfixExpression LEFTPAREN argumentExpressionList? RIGHTPAREN
| postfixExpression DOT IDENTIFIER
| postfixExpression ARROW IDENTIFIER
| postfixExpression PLUSPLUS
| postfixExpression MINUSMINUS
| LEFTPAREN typeName RIGHTPAREN LEFTBRACE initializerList RIGHTBRACE
| LEFTPAREN typeName RIGHTPAREN LEFTBRACE initializerList COMMA
RIGHTBRACE
| EXTENSION LEFTPAREN typeName RIGHTPAREN LEFTBRACE initializerList
RIGHTBRACE
| EXTENSION LEFTPAREN typeName RIGHTPAREN LEFTBRACE initializerList
COMMA RIGHTBRACE
;
argumentExpressionList
: asSIGNmentExpression
| argumentExpressionList COMMA asSIGNmentExpression
;
unaryExpression
: postfixExpression
| PLUSPLUS unaryExpression
| MINUSMINUS unaryExpression
| unaryOperator castExpression
| SIZEOF unaryExpression
| SIZEOF LEFTPAREN typeName RIGHTPAREN
| ALIGNOF LEFTPAREN typeName RIGHTPAREN
| ANDAND IDENTIFIER // GCC extension address of label
;
unaryOperator
: AND | STAR | PLUS | MINUS | TILDE | NOT
;
castExpression
: unaryExpression
| LEFTPAREN typeName RIGHTPAREN castExpression
| EXTENSION LEFTPAREN typeName RIGHTPAREN castExpression
;
multiplicativeExpression
: castExpression
| multiplicativeExpression STAR castExpression
| multiplicativeExpression DIV castExpression
| multiplicativeExpression MOD castExpression
;
additiveExpression
: multiplicativeExpression
| additiveExpression PLUS multiplicativeExpression
| additiveExpression MINUS multiplicativeExpression
;
shiftExpression
: additiveExpression
| shiftExpression LEFTSHIFT additiveExpression
| shiftExpression RIGHTSHIFT additiveExpression
;
relationalExpression
: shiftExpression
| relationalExpression LESS shiftExpression
| relationalExpression GREATER shiftExpression
| relationalExpression LESSEQUAL shiftExpression
| relationalExpression GREATEREQUAL shiftExpression
;
equalityExpression
: relationalExpression
| equalityExpression EQUAL relationalExpression
| equalityExpression NOTEQUAL relationalExpression
;
andExpression
: equalityExpression
| andExpression AND equalityExpression
;
exclusiveORExpression
: andExpression
| exclusiveORExpression CARET andExpression
;
inclusiveORExpression
: exclusiveORExpression
| inclusiveORExpression OR exclusiveORExpression
;
logicalAndExpression
: inclusiveORExpression
| logicalAndExpression ANDAND inclusiveORExpression
;
logicalORExpression
: logicalAndExpression
| logicalORExpression OROR logicalAndExpression
;
conditionalExpression
: logicalORExpression (QUESTION expression COLON
conditionalExpression)?
;
asSIGNmentExpression
: conditionalExpression
| unaryExpression asSIGNmentOperator asSIGNmentExpression
;
asSIGNmentOperator
: ASSIGN | STARASSIGN | DIVASSIGN | MODASSIGN | PLUSASSIGN |
MINUSASSIGN | LEFTSHIFTASSIGN | RIGHTSHIFTASSIGN | ANDASSIGN | XORASSIGN |
ORASSIGN
;
expression
: asSIGNmentExpression
| expression COMMA asSIGNmentExpression
;
constantExpression
: conditionalExpression
;
declaration
: declarationSpecifiers initDeclaratorList? SEMI
| staticAssertDeclaration
;
declarationSpecifiers
: declarationSpecifier+
;
declarationSpecifiers2
: declarationSpecifier+
;
declarationSpecifier
: storageClassSpecifier
| typeSpecifier
| typeQualifier
| functionSpecifier
| alignmentSpecifier
;
initDeclaratorList
: initDeclarator
| initDeclaratorList COMMA initDeclarator
;
initDeclarator
: declarator
| declarator ASSIGN initializer
;
storageClassSpecifier
: TYPEDEF
| EXTER
| STATIC
| THREADLOCAL
| AUTO
| REGISTER
;
typeSpecifier
: (VOID
| CHAR
| SHORT
| INT
| LONG
| FLOAT
| DOUBLE
| SIGNED
| UNSIGNED
| BOOL
| COMPLEX
| M128)
| EXTENSION LEFTPAREN M128 RIGHTPAREN
| atomicTypeSpecifier
| structORUnionSpecifier
| enumSpecifier
| typedefName
| TYPEOF LEFTPAREN constantExpression RIGHTPAREN // GCC extension
;
structORUnionSpecifier
: structORUnion IDENTIFIER? LEFTBRACE structDeclarationList RIGHTBRACE
| structORUnion IDENTIFIER
;
structORUnion
: STRUCT
| UNION
;
structDeclarationList
: structDeclaration
| structDeclarationList structDeclaration
;
structDeclaration
: specifierQualifierList structDeclaratorList? SEMI
| staticAssertDeclaration
;
specifierQualifierList
: typeSpecifier specifierQualifierList?
| typeQualifier specifierQualifierList?
;
structDeclaratorList
: structDeclarator
| structDeclaratorList COMMA structDeclarator
;
structDeclarator
: declarator
| declarator? COLON constantExpression
;
enumSpecifier
: ENUM IDENTIFIER? LEFTBRACE enumeratorList RIGHTBRACE
| ENUM IDENTIFIER? LEFTBRACE enumeratorList COMMA RIGHTBRACE
| ENUM IDENTIFIER
;
enumeratorList
: enumerator
| enumeratorList COMMA enumerator
;
enumerator
: enumerationConstant
| enumerationConstant ASSIGN constantExpression
;
enumerationConstant
: IDENTIFIER
;
atomicTypeSpecifier
: ATOMIC LEFTPAREN typeName RIGHTPAREN
;
typeQualifier
: CONST
| RESTRICT
| VOLATILE
| ATOMIC
;
functionSpecifier
: (INLINE
| NORETURN
| INLINE // GCC extension
| STDCALL)
| gccAttributeSpecifier
| DECLSPEC LEFTPAREN IDENTIFIER RIGHTPAREN
;
alignmentSpecifier
: ALIGNAS LEFTPAREN typeName RIGHTPAREN
| ALIGNAS LEFTPAREN constantExpression RIGHTPAREN
;
declarator
: pointer? directDeclarator gccDeclaratorExtension*
;
directDeclarator
: IDENTIFIER
| LEFTPAREN declarator RIGHTPAREN
| directDeclarator LEFTBRACKET typeQualifierList?
asSIGNmentExpression? RIGHTBRACKET
| directDeclarator LEFTBRACKET STATIC typeQualifierList?
asSIGNmentExpression RIGHTBRACKET
| directDeclarator LEFTBRACKET typeQualifierList STATIC
asSIGNmentExpression RIGHTBRACKET
| directDeclarator LEFTBRACKET typeQualifierList? STAR RIGHTBRACKET
| directDeclarator LEFTPAREN parameterTypeList RIGHTPAREN
| directDeclarator LEFTPAREN identifierList? RIGHTPAREN
;
gccDeclaratorExtension
: ASM LEFTPAREN STRINGLITERAL+ RIGHTPAREN
| gccAttributeSpecifier
;
gccAttributeSpecifier
: ATTRIBUTE LEFTPAREN LEFTPAREN gccAttributeList RIGHTPAREN RIGHTPAREN
;
gccAttributeList
: gccAttribute (COMMA gccAttribute)*
| // empty
;
gccAttribute
: ~(COMMA | LEFTPAREN | RIGHTPAREN) // relaxed def for "IDENTIFIER or
reserved word"
(LEFTPAREN argumentExpressionList? RIGHTPAREN)?
| // empty
;
nestedParenthesesBlock
: ( ~(LEFTPAREN | RIGHTPAREN)
| LEFTPAREN nestedParenthesesBlock RIGHTPAREN
)*
;
pointer
: STAR typeQualifierList?
| STAR typeQualifierList? pointer
| CARET typeQualifierList? // Blocks language extension
| CARET typeQualifierList? pointer // Blocks language extension
;
typeQualifierList
: typeQualifier
| typeQualifierList typeQualifier
;
parameterTypeList
: parameterList
| parameterList COMMA ELLIPSIS
;
parameterList
: parameterDeclaration
| parameterList COMMA parameterDeclaration
;
parameterDeclaration
: declarationSpecifiers declarator
| declarationSpecifiers2 abstractDeclarator?
;
identifierList
: IDENTIFIER
| identifierList COMMA IDENTIFIER
;
typeName
: specifierQualifierList abstractDeclarator?
;
abstractDeclarator
: pointer
| pointer? directAbstractDeclarator gccDeclaratorExtension*
;
directAbstractDeclarator
: LEFTPAREN abstractDeclarator RIGHTPAREN gccDeclaratorExtension*
| LEFTBRACKET typeQualifierList? asSIGNmentExpression? RIGHTBRACKET
| LEFTBRACKET STATIC typeQualifierList? asSIGNmentExpression
RIGHTBRACKET
| LEFTBRACKET typeQualifierList STATIC asSIGNmentExpression
RIGHTBRACKET
| LEFTBRACKET STAR RIGHTBRACKET
| LEFTPAREN parameterTypeList? RIGHTPAREN gccDeclaratorExtension*
| directAbstractDeclarator LEFTBRACKET typeQualifierList?
asSIGNmentExpression? RIGHTBRACKET
| directAbstractDeclarator LEFTBRACKET STATIC typeQualifierList?
asSIGNmentExpression RIGHTBRACKET
| directAbstractDeclarator LEFTBRACKET typeQualifierList STATIC
asSIGNmentExpression RIGHTBRACKET
| directAbstractDeclarator LEFTBRACKET STAR RIGHTBRACKET
| directAbstractDeclarator LEFTPAREN parameterTypeList? RIGHTPAREN
gccDeclaratorExtension*
;
typedefName
: IDENTIFIER
;
initializer
: asSIGNmentExpression
| LEFTBRACE initializerList RIGHTBRACE
| LEFTBRACE initializerList COMMA RIGHTBRACE
;
initializerList
: deSIGNation? initializer
| initializerList COMMA deSIGNation? initializer
;
deSIGNation
: deSIGNatorList ASSIGN
;
deSIGNatorList
: deSIGNator
| deSIGNatorList deSIGNator
;
deSIGNator
: LEFTBRACKET constantExpression RIGHTBRACKET
| DOT IDENTIFIER
;
staticAssertDeclaration
: STATICASSERT LEFTPAREN constantExpression COMMA STRINGLITERAL+
RIGHTPAREN SEMI
;
statement
: labeledStatement
| compoundStatement
| expressionStatement
| selectionStatement
| iterationStatement
| jumpStatement
| ASM VOLATILE LEFTPAREN (logicalORExpression (COMMA
logicalORExpression)_)? (COLON (logicalORExpression (COMMA
logicalORExpression)_)?)* RIGHTPAREN SEMI
;
labeledStatement
: IDENTIFIER COLON statement
| CASE constantExpression COLON statement
| DEFAULT COLON statement
;
compoundStatement
: LEFTBRACE blockItemList? RIGHTBRACE
;
blockItemList
: blockItem
| blockItemList blockItem
;
blockItem
: declaration
| statement
;
expressionStatement
: expression? SEMI
;
selectionStatement
: IF LEFTPAREN expression RIGHTPAREN statement (ELSE statement)?
| SWITCH LEFTPAREN expression RIGHTPAREN statement
;
iterationStatement
: WHILE LEFTPAREN expression RIGHTPAREN statement
| DO statement WHILE LEFTPAREN expression RIGHTPAREN SEMI
| FOR LEFTPAREN expression? SEMI expression? SEMI expression?
RIGHTPAREN statement
| FOR LEFTPAREN declaration expression? SEMI expression? RIGHTPAREN
statement
;
jumpStatement
: GOTO IDENTIFIER SEMI
| CONTINUE SEMI
| BREAK SEMI
| RETURN expression? SEMI
| GOTO unaryExpression SEMI // GCC extension
;
compilationUnit
: translationUnit? EOF
;
translationUnit
: externalDeclaration
| translationUnit externalDeclaration
;
externalDeclaration
: functionDefinition
| declaration
| SEMI // stray ;
;
functionDefinition
: declarationSpecifiers? declarator declarationList? compoundStatement
;
declarationList
: declaration
| declarationList declaration
;
// LEXER RULES
AUTO : 'auto';
BREAK : 'break';
CASE : 'case';
CHAR : 'char';
CONST : 'const';
CONTINUE : 'continue';
DEFAULT : 'default';
DO : 'do';
DOUBLE : 'double';
ELSE : 'else';
ENUM : 'enum';
EXTER : 'extern';
FLOAT : 'float';
FOR : 'for';
GOTO : 'goto';
IF : 'if';
INLINE : 'inline'
| 'inline'
;
INT : 'int';
LONG : 'long';
REGISTER : 'register';
RESTRICT : 'restrict';
RETURN : 'return';
SHORT : 'short';
SIGNED : 'SIGNed';
SIZEOF : 'sizeof';
STATIC : 'static';
STRUCT : 'struct';
SWITCH : 'switch';
TYPEDEF : 'typedef';
UNION : 'union';
UNSIGNED : 'unSIGNed';
VOID : 'void';
VOLATILE : 'volatile'
| 'volatile';
WHILE : 'while';
ALIGNAS : '_Alignas';
ALIGNOF : '_Alignof';
ATOMIC : '_Atomic';
BOOL : '_Bool';
COMPLEX : '_Complex';
GENERIC : '_Generic';
IMAGINARY : '_Imaginary';
NORETURN : '_Noreturn';
STATICASSERT : '_Static_assert';
THREADLOCAL : '_Thread_local';
LEFTPAREN : '(' ;
RIGHTPAREN : ')' ;
LEFTBRACKET : '[' ;
RIGHTBRACKET : ']';
LEFTBRACE : '{';
RIGHTBRACE : '}' ;
LESS : '<';
LESSEQUAL : '<=';
GREATER : '>';
GREATEREQUAL : '>=';
LEFTSHIFT : '<<';
RIGHTSHIFT : '>>';
PLUS : '+';
PLUSPLUS : '++';
MINUS : '-';
MINUSMINUS : '--';
STAR : '*';
DIV : '/';
MOD : '%';
AND : '&';
OR : '|';
ANDAND : '&&';
OROR: '||';
CARET : '^';
NOT : '!';
TILDE : '~';
QUESTION : '?';
COLON : ':';
SEMI : ';' ;
COMMA : ',' ;
ASSIGN : '=';
// '_=' | '/=' | '%=' | '+=' | '-=' | '<<=' | '>>=' | '&=' | '^=' | '|='
STARASSIGN : '_=';
DIVASSIGN : '/=';
MODASSIGN : '%=';
PLUSASSIGN : '+=';
MINUSASSIGN : '-=';
LEFTSHIFTASSIGN : '<<=';
RIGHTSHIFTASSIGN : '>>=';
ANDASSIGN : '&=';
XORASSIGN : '^=';
ORASSIGN : '|=';
EQUAL : '==';
NOTEQUAL : '!=';
ARROW : '->';
DOT : '.';
ELLIPSIS : '...';
EXTENSION : 'extension' ;
BUILTINVAARG : 'builtin_va_arg' ;
BUILTINOFFSETOF : '__builtin_offsetof' ;
M128 : '__m128'
| '__m128d'
| '__m128i'
;
ASM : ('__asm' | '__asm') ;
TYPEOF : 'typeof' ;
STDCALL : 'stdcall';
DECLSPEC : '__declspec' ;
ATTRIBUTE : '__attribute' ;
IDENTIFIER
: IDENTIFIERNONDIGIT
( IDENTIFIERNONDIGIT
| DIGIT
)*
;
fragment
IDENTIFIERNONDIGIT
: NONDIGIT
| UNIVERSALCHARACTERNAME
//| // other implementation-defined characters...
;
fragment
NONDIGIT
: [a-zA-Z_]
;
fragment
DIGIT
: [0-9]
;
fragment
UNIVERSALCHARACTERNAME
: 'u' HEXQUAD
| 'U' HEXQUAD HEXQUAD
;
fragment
HEXQUAD
: HEXADECIMALDIGIT HEXADECIMALDIGIT HEXADECIMALDIGIT HEXADECIMALDIGIT
;
CONSTANT
: INTEGERCONSTANT
| FLOATINGCONSTANT
//| EnumerationConstant
| CHARACTERCONSTANT
;
fragment
INTEGERCONSTANT
: DECIMALCONSTANT INTEGERSUFFIX?
| OCTALCONSTANT INTEGERSUFFIX?
| HEXADECIMALCONSTANT INTEGERSUFFIX?
;
fragment
DECIMALCONSTANT
: NONZERODIGIT DIGIT*
;
fragment
OCTALCONSTANT
: '0' OCTALDIGIT*
;
fragment
HEXADECIMALCONSTANT
: HEXADECIMALPREFIX HEXADECIMALDIGIT+
;
fragment
HEXADECIMALPREFIX
: '0' [xX]
;
fragment
NONZERODIGIT
: [1-9]
;
fragment
OCTALDIGIT
: [0-7]
;
fragment
HEXADECIMALDIGIT
: [0-9a-fA-F]
;
fragment
INTEGERSUFFIX
: UNSIGNEDSUFFIX LONGSUFFIX?
| UNSIGNEDSUFFIX LONGLONGSUFFIX
| LONGSUFFIX UNSIGNEDSUFFIX?
| LONGLONGSUFFIX UNSIGNEDSUFFIX?
;
fragment
UNSIGNEDSUFFIX
: [uU]
;
fragment
LONGSUFFIX
: [lL]
;
fragment
LONGLONGSUFFIX
: 'll' | 'LL'
;
fragment
FLOATINGCONSTANT
: DECIMALFLOATINGCONSTANT
| HEXADECIMALFLOATINGCONSTANT
;
fragment
DECIMALFLOATINGCONSTANT
: FRACTIONALCONSTANT EXPONENTPART? FLOATINGSUFFIX?
| DIGITSEQUENCE EXPONENTPART FLOATINGSUFFIX?
;
fragment
HEXADECIMALFLOATINGCONSTANT
: HEXADECIMALPREFIX HEXADECIMALFRACTIONALCONSTANT BINARYEXPONENTPART
FLOATINGSUFFIX?
| HEXADECIMALPREFIX HEXADECIMALDIGITSEQUENCE BINARYEXPONENTPART
FLOATINGSUFFIX?
;
fragment
FRACTIONALCONSTANT
: DIGITSEQUENCE? '.' DIGITSEQUENCE
| DIGITSEQUENCE '.'
;
fragment
EXPONENTPART
: 'e' SIGN? DIGITSEQUENCE
| 'E' SIGN? DIGITSEQUENCE
;
fragment
SIGN
: '+' | '-'
;
fragment
DIGITSEQUENCE
: DIGIT+
;
fragment
HEXADECIMALFRACTIONALCONSTANT
: HEXADECIMALDIGITSEQUENCE? '.' HEXADECIMALDIGITSEQUENCE
| HEXADECIMALDIGITSEQUENCE '.'
;
fragment
BINARYEXPONENTPART
: 'p' SIGN? DIGITSEQUENCE
| 'P' SIGN? DIGITSEQUENCE
;
fragment
HEXADECIMALDIGITSEQUENCE
: HEXADECIMALDIGIT+
;
fragment
FLOATINGSUFFIX
: 'f' | 'l' | 'F' | 'L'
;
fragment
CHARACTERCONSTANT
: ''' CCHARSEQUENCE '''
| 'L'' CCHARSEQUENCE '''
| 'u'' CCHARSEQUENCE '''
| 'U'' CCHARSEQUENCE '''
;
fragment
CCHARSEQUENCE
: CCHAR+
;
fragment
CCHAR
: ~['\rn]
| ESCAPESEQUENCE
;
fragment
ESCAPESEQUENCE
: SIMPLEESCAPESEQUENCE
| OCATALESCAPESEQUENCE
| HEXADECIMALESCAPESEQUENCE
| UNIVERSALCHARACTERNAME
;
fragment
SIMPLEESCAPESEQUENCE
: '' ['"?abfnrtv]
;
fragment
OCATALESCAPESEQUENCE
: '' OCTALDIGIT
| '' OCTALDIGIT OCTALDIGIT
| '' OCTALDIGIT OCTALDIGIT OCTALDIGIT
;
fragment
HEXADECIMALESCAPESEQUENCE
: 'x' HEXADECIMALDIGIT+
;
STRINGLITERAL
: ENCODINGPREFIX? '"' SCHARSEQUENCE? '"'
;
fragment
ENCODINGPREFIX
: 'u8'
| 'u'
| 'U'
| 'L'
;
fragment
SCHARSEQUENCE
: SCHAR+
;
fragment
SCHAR
: ~["\rn]
| ESCAPESEQUENCE
;
LINEDIRECTIVE
: '#' WHITESPACE? DECIMALCONSTANT WHITESPACE? STRINGLITERAL ~[rn]*
-> skip
;
PRAGMADIERECTIVE
: '#' WHITESPACE? 'pragma' WHITESPACE ~[rn]*
-> skip
;
WHITESPACE
: [ t]+
-> skip
;
NEWLINE
: ( 'r' 'n'?
| 'n'
)
-> skip
;
BLOCKCOMMENT
: '/_' ._? '*/'
-> skip
;
LINECOMMENT
: '//' ~[rn]*
-> skip
;
On Sun, May 24, 2015 at 8:33 PM, Terence Parr [email protected]
wrote:
see faq link
—
Reply to this email directly or view it on GitHub
https://github.com/antlr/antlr4/issues/147#issuecomment-105046562.
Hi Terrance,
I have seen the FAQ. I have no problem in parser generator, java
compilation and using grun command to see output. My only problem is the
compilation unit. When I add it my program is hanged and computer resource
consumption is at it's maximum limit. It returns nothing. I have also tried
to execture it with direct input:
grun cpp program -tree
int main () {
printf();
}
ctrl-z
on windows and as well as on linux but results are same.
I do not know how to correct this issue.
Best Regards,
Nazish Asad
On Sun, May 24, 2015 at 8:33 PM, Terence Parr [email protected]
wrote:
see faq link
—
Reply to this email directly or view it on GitHub
https://github.com/antlr/antlr4/issues/147#issuecomment-105046562.
i've no idea
I have figured out the problem. compilationUnit itself is the main rule of the grammar. No need to add anything into the grammar.
grun C compilationUnit -tree Test.c will generate the output.
I am trying to run the simplest Hello.g4 grammar and I am getting the error "Can't load Hello as lexer or parser"
I am running it on windows and my CLASSPATH has the dot.
I really don't know where i am going wrong
Thanks
No it doesn't: "I am running it on windows and my CLASSPATH has the dot."
dare i ask if you know java code needs to be compiled?
On Jul 31, 2015, at 5:29 AM, Alex Tilkin [email protected] wrote:
Hi,
I'm having the "Can't load Hello as lexer or parser" issue and I do have the '.' in the CLASSPATH.
Can you help me please? @parrt
Thank you...
—
Reply to this email directly or view it on GitHub.
@parrt, yesterday, 10 minutes after posting the comment I solved the issue and deleted it from the thread. It was exactly what you pointed out. Thank you for your quick response. :)
hello @parrt
im wondering if you know any way to run the grun on java? i can run the code in command prompt though it doesn't popup as a tree but a line with many brackets (i think its BNF?)
nah if i want to use it's result in java, what should i do to run the grun?
this is my line of code.
`/*
package cmdtest;
/**
*
public class CmdTest {
public static void main(String[] args) throws Exception {
ProcessBuilder builder = new ProcessBuilder(
"cmd.exe", "/c", "cd.. && antlr4 Java.g4 && javac Java*.java && grun java compilationUnit -tree");
builder.redirectErrorStream(true);
Process p = builder.start();
BufferedReader r = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line;
while (true) {
line = r.readLine();
if (line == null) { break; }
System.out.println(line);
}
}
}
and it has the result :
D:skripsijar>java org.antlr.v4.Tool Java.g4
D:skripsijar>java org.antlr.v4.runtime.misc.TestRig java compilationUnit -tree
Warning: TestRig moved to org.antlr.v4.gui.TestRig; calling automatically
Problems calling org.antlr.v4.gui.TestRig.main(args)`
Thanks anyway :+1:
Hello Parrt,
Could I ask what does this mean?
try EOF (ctrl-D or ctrl-Z) on line by itself if reading from stdin. You will find the FAQ oh so useful folks. https://theantlrguy.atlassian.net/wiki/pages/viewpage.action?pageId=7143433
This link die and I do not know what to look at.
I also have the infinite loop, I think. When I use grun it stuck and show nothing...
I am looking forward for your reply,
Thanks :)
Nana,
hit ctrl-Z to end the input to stdin.
Hello Parr,
Thank you very much for your response. Where should I put the Ctrl-Z? It doesn't work either way...
I am looking forward for your reply ^^
Thanks,
Nana
Try it on the line by itself
Sent from my iPhone
On Jul 17, 2016, at 5:19 PM, nana88 [email protected] wrote:
Hello Parr,
Thank you very much for your response. Where should I put the Ctrl-Z? It doesn't work either way...
I am looking forward for your reply ^^
Thanks,
Nana—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub, or mute the thread.
Hello Parr,
I figured it out. Thank you very much ^^
Nana,
Hi,
this is not the place for support, please close this and go to the google discussion group
Eric
Le 30 août 2016 à 17:05, IhorHoivaniuk [email protected] a écrit :
I made everything what is written in install guide(https://github.com/sharwell/antlr4cs/wiki https://github.com/sharwell/antlr4cs/wiki) but NATLR doesn't work for me. An empty HelloParser and HelloLexer are generated and that's all ... Could you suggest me what is wrong ?
—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub https://github.com/antlr/antlr4/issues/147#issuecomment-243471072, or mute the thread https://github.com/notifications/unsubscribe-auth/ADLYJCo5ksWnpyaFmMeREYymmrVTJlvnks5qlEbLgaJpZM4AYJ1r.
Hi,
I want to generate parse tree that is generating in eclipse parse tree diagram. How can I generate that type of tree.
??
Hi,
this is not the place for support, please join the google discussion group and close this.
Eric
Le 29 sept. 2016 à 02:07, haridwar-jha [email protected] a écrit :
Hi,
I want to generate parse tree that is generating in eclipse parse tree diagram. How can I generate that type of tree.
??
https://cloud.githubusercontent.com/assets/20932577/18926195/836895c8-85d4-11e6-9ece-9ee2f3358481.png
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub https://github.com/antlr/antlr4/issues/147#issuecomment-250249949, or mute the thread https://github.com/notifications/unsubscribe-auth/ADLYJK5E_92PNhIi0zV8KbtBXx0GN6Bhks5quqzvgaJpZM4AYJ1r.
On windows 10, for people ending up here after having grun saying
can't load stuff as lexer or parser
Use the correct interface to change the environment variable, as such:
[Win key], [e],[n],[v] (otherwise said, search for "env" in the start menu)
As you can see, Microsoft added a version where system vars are locked out, so it's important to grab the good one. Then, "environment varaibles", then Path
on both user and system must point to a consistent Java SDK installation.
Finally, CLASSPATH
will show up like this:
I created a new "." entry and moved it up. Because we don't have access to raw line edit anymore. stupid GUI.
After that, you can go with the technique of regedit
your Computer\HKEY_CURRENT_USER\Software\Microsoft\Command Processor
and add a AutoRun
key with this content: %HOMEPATH%\bashrc.bat
And then fill your bashrc.bat
with:
@doskey antlr4=java org.antlr.v4.Tool $*
@doskey grun =java org.antlr.v4.gui.TestRig $*
Finally, after you compiled your grammar with the antlr4
command, you need to compile the *.java with javac.exe !
Make sure your javac
is accessible and runs the same version.
prompt> javac --version
prompt> java --version
It wasn't the case for me, because of many java-jre present and conflicting.
You can check that by:
prompt> where java
For me it returns
C:\Program Files\Java\jdk-11.0.1\bin\java.exe
After you have that sorted out, do prompt> javac yourgrammar*.java
And finally calling grun
will work.
bests !
Thanks @siliconvoodoo could you make a pull request on the doc page https://github.com/antlr/antlr4/blob/master/doc/getting-started.md ?
@parrt, yesterday, 10 minutes after posting the comment I solved the issue and deleted it from the thread. It was exactly what you pointed out. Thank you for your quick response. :)
How do you solved it?