Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sample doesn't work on Windows environment. #147

Closed
tarantsoff opened this issue Jan 22, 2013 · 44 comments
Closed

Sample doesn't work on Windows environment. #147

tarantsoff opened this issue Jan 22, 2013 · 44 comments
Assignees
Milestone

Comments

@tarantsoff
Copy link

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
...

2013-01-23_0033


The content of grammar file I've just copy-pasted from web page.

@parrt
Copy link
Member

parrt commented Jan 22, 2013

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.

@tarantsoff
Copy link
Author

Yes, it was exactly classpath issue. Thank you, and sorry for my panic :)

@tarantsoff
Copy link
Author

now it works better. not ideal - because reports error but better.
Uploading 2013-01-23_0242.png . . .

@tarantsoff
Copy link
Author

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>

@ghost ghost assigned parrt Jan 22, 2013
@sharwell
Copy link
Member

The WS rule of the grammar needed \r added to it. I updated the Getting Started page to reflect.

@tarantsoff
Copy link
Author

yes, \r helps and now sample works nice. Thank you :)

@iyawnis
Copy link

iyawnis commented Feb 20, 2013

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.

@sharwell
Copy link
Member

Did you edit the Hello.g4 file as mentioned in the comments above?

@BR99
Copy link

BR99 commented Sep 25, 2013

Hi
I'm facing same issue. followed the instructions but still need good advice

D:\ANTLR>echo %CLASSPATH%
D:\ANTLR\antlr-4.0-complete.jar;.

D:\ANTLR>java -cp D:\ANTLR\antlr-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 : [ \t\r\n]+ -> skip ;

Thanks for any help !!

@parrt
Copy link
Member

parrt commented Sep 25, 2013

feel free to compile the source :)

@BR99
Copy link

BR99 commented Sep 25, 2013

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:notifications@github.com]
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 #147 (comment) . https://github.com/notifications/beacon/6kp6OnXcvE3U-_DAG9_vmagTY8ENz-uEYGdPXhGbmqwt-GtF0ueJAoHfH738TshD.gif

@parrt
Copy link
Member

parrt commented Sep 25, 2013

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.

@BR99
Copy link

BR99 commented Sep 25, 2013

Thanks a lot

Ben

From: Terence Parr [mailto:notifications@github.com]
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 #147 (comment) . https://github.com/notifications/beacon/6kp6OnXcvE3U-_DAG9_vmagTY8ENz-uEYGdPXhGbmqwt-GtF0ueJAoHfH738TshD.gif

@alfonsonishikawa
Copy link

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

@parrt
Copy link
Member

parrt commented Mar 10, 2014

hahahah. no problem. Good luck!

@b-grider
Copy link

I'm having a similar problem and can't figure out what's wrong. Help anyone?
antlrerror

@parrt
Copy link
Member

parrt commented May 23, 2014

dot in classpath. is that missing in the getting started?

@b-grider
Copy link

Of course. That was the problem. I thought it was hanging and didn't realize it was waiting for input. Thank you.

@RockManJoe64
Copy link

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).

@pbajaj14
Copy link

i have also similar issue.......
Grun just struck at one point and not showing anything

@parrt
Copy link
Member

parrt commented May 19, 2015

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

@nazishasad
Copy link

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.

@parrt
Copy link
Member

parrt commented May 24, 2015

see faq link

@nazishasad
Copy 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.

@nazishasad
Copy link

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:

  1. Redistributions of source code must retain the above copyright
    notice, this list of conditions and the following disclaimer.

  2. Redistributions in binary form must reproduce the above copyright
    notice, this list of conditions and the following disclaimer in the
    documentation and/or other materials provided with the distribution.

  3. The name of the author may not be used to endorse or promote products
    derived from this software without specific prior written permission.

    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
: ~['\r\n]
| 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
: ~["\r\n]
| ESCAPESEQUENCE
;

LINEDIRECTIVE
: '#' WHITESPACE? DECIMALCONSTANT WHITESPACE? STRINGLITERAL ~[\r\n]*
-> skip
;

PRAGMADIERECTIVE
: '#' WHITESPACE? 'pragma' WHITESPACE ~[\r\n]*
-> skip
;

WHITESPACE
: [ \t]+
-> skip
;

NEWLINE
: ( '\r' '\n'?
| '\n'
)
-> skip
;

BLOCKCOMMENT
: '/' .? '*/'
-> skip
;

LINECOMMENT
: '//' ~[\r\n]*
-> skip
;

On Sun, May 24, 2015 at 8:33 PM, Terence Parr notifications@github.com
wrote:

see faq link


Reply to this email directly or view it on GitHub
#147 (comment).

@nazishasad
Copy link

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 notifications@github.com
wrote:

see faq link


Reply to this email directly or view it on GitHub
#147 (comment).

@parrt
Copy link
Member

parrt commented May 24, 2015

i've no idea

@nazishasad
Copy link

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.

@avnibharne
Copy link

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.
failure
I really don't know where i am going wrong
Thanks

@parrt
Copy link
Member

parrt commented Jun 12, 2015

No it doesn't: "I am running it on windows and my CLASSPATH has the dot."

@parrt
Copy link
Member

parrt commented Jul 31, 2015

dare i ask if you know java code needs to be compiled?

On Jul 31, 2015, at 5:29 AM, Alex Tilkin notifications@github.com 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.

@alex-tilkin
Copy link

@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. :)

@vaniachoi
Copy link

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.

`/*

  • To change this license header, choose License Headers in Project Properties.
  • To change this template file, choose Tools | Templates
  • and open the template in the editor.
    */

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:\skripsi\jar>java org.antlr.v4.Tool Java.g4

D:\skripsi\jar>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 👍

@nana88
Copy link

nana88 commented Jul 15, 2016

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...

parser

I am looking forward for your reply,
Thanks :)
Nana,

@parrt
Copy link
Member

parrt commented Jul 15, 2016

hit ctrl-Z to end the input to stdin.

@nana88
Copy link

nana88 commented Jul 18, 2016

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
antlr

@parrt
Copy link
Member

parrt commented Jul 18, 2016

Try it on the line by itself

Sent from my iPhone

On Jul 17, 2016, at 5:19 PM, nana88 notifications@github.com 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.

@nana88
Copy link

nana88 commented Jul 18, 2016

Hello Parr,

I figured it out. Thank you very much ^^

Nana,

@ericvergnaud
Copy link
Contributor

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 notifications@github.com 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 #147 (comment), or mute the thread https://github.com/notifications/unsubscribe-auth/ADLYJCo5ksWnpyaFmMeREYymmrVTJlvnks5qlEbLgaJpZM4AYJ1r.

@harry253
Copy link

Hi,
I want to generate parse tree that is generating in eclipse parse tree diagram. How can I generate that type of tree.
??
parse_tree

@ericvergnaud
Copy link
Contributor

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 notifications@github.com 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 #147 (comment), or mute the thread https://github.com/notifications/unsubscribe-auth/ADLYJK5E_92PNhIi0zV8KbtBXx0GN6Bhks5quqzvgaJpZM4AYJ1r.

@siliconvoodoo
Copy link

siliconvoodoo commented Oct 19, 2018

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)
image
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:
image
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 !

@parrt
Copy link
Member

parrt commented Oct 19, 2018

Thanks @siliconvoodoo could you make a pull request on the doc page https://github.com/antlr/antlr4/blob/master/doc/getting-started.md ?

@MSalehie
Copy link

@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?

KvanTTT pushed a commit to KvanTTT/antlr4 that referenced this issue Jan 22, 2024
fix antlr#144: indicate the change of license

Co-authored-by: Edoardo Luppi <lp.edoardo@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests