Convert a form of BNF grammar to g4 grammar









up vote
0
down vote

favorite












I'm trying to covert this BNF grammar for validating boolean expression which is currently being used in Swift. Now want to implement similar parser in Java. I came across Antlr4 lib and want to use it to generate parser for the same grammar. I'm not very familiar with antlr4. Can someone please provide me some guidance? This is what I have so far.



Expr ::= <ConcatenationExpr>;
NonInfixExpr ::= <BracketExpr>
| <Function>
| <Literal>
| <Accessor>;
BracketExpr ::= '(' <Expr> ')';
Accessor ::= ('$' <AccessorComponent> | <AccessorComponent>) ('.' <AccessorComponent> )*;
AccessorComponent ::= 'Identifier' ':' 'Identifier' | 'Identifier';
Function ::= 'Identifier' '(' ')' | ('Identifier' '(' <Expr> (',' <Expr>)* ')');
Literal ::= 'True'
| 'False'
| 'Null'
| 'Number'
| 'Text';
ConcatenationExpr ::= <AndExpr>
| <ConcatenationExpr> '&' <AndExpr>;
AndExpr ::= <OrExpr>
| <AndExpr> '&&' <OrExpr>;
OrExpr ::= <EqualityExpr>
| <OrExpr> '||' <EqualityExpr>;
EqualityExpr ::= <ComparisonExpr>
| <EqualityExpr> ('==' | '=' | '!=' | '<>') <ComparisonExpr>;
ComparisonExpr ::= <AddExpr>
| <AddExpr> ('<' | '<=' | '>' | '>=') <AddExpr>;
AddExpr ::= <ExponentialExpr>
| <AddExpr> ('+' | '-') <ExponentialExpr>;
ExponentialExpr ::= <MultExpr>
| <ExponentialExpr> '^' <MultExpr>;
MultExpr ::= <NonInfixExpr>
| <MultExpr> ('*' | '/') <NonInfixExpr>;


I tried to convert to g4 and this is what it looks like.



grammar VALIDATE;

Expr
: ConcatenationExpr ';'
;

NonInfixExpr
: BracketExpr
| Function
| Literal
| Accessor
;

BracketExpr
: '(' Expr ')'
;

Accessor
: ('$' AccessorComponent | AccessorComponent ) ('.' AccessorComponent )*
;

AccessorComponent
: 'Identifier' ':' 'Identifier' | 'Identifier'
;

Function
: 'Identifier' '(' ')' | ('Identifier' '(' Expr (',' Expr)* ')')
;

Literal
: 'True' | 'False' | 'Null' | 'Number' | 'Text'
;

ConcatenationExpr
: AndExpr
| ConcatenationExpr '&&' AndExpr
;

AndExpr
: OrExpr | AndExpr '&&' OrExpr
;

OrExpr
: EqualityExpr | OrExpr '||' EqualityExpr
;

EqualityExpr
: ComparisonExpr | EqualityExpr ('==' | '=' | '!=' | '<>') ComparisonExpr
;

ComparisonExpr
: AddExpr | AddExpr ('<' | '<=' | '>' | '>=') AddExpr
;

AddExpr
: ExponentialExpr | AddExpr ('+' | '-') ExponentialExpr
;

ExponentialExpr
: MultExpr | ExponentialExpr '^' MultExpr
;

MultExpr
: NonInfixExpr | MultExpr ('*' | '/') NonInfixExpr
;


I'm stuck at antlr4 VALIDATE.g4 step. I'm not sure I did the conversion right.



error(119): VALIDATE.g4::: The following sets of rules are mutually left-recursive [MultExpr] and [ExponentialExpr] and [AddExpr] and [EqualityExpr] and [OrExpr] and [AndExpr] and [ConcatenationExpr]
error(99): VALIDATE.g4::: grammar VALIDATE has no rules









share|improve this question





















  • ANTLR does not support left-recursion across multiple rules. You'll need to either combine all your expression rules into a single rule Expr with the alternatives sorted by precedence; or rewrite the grammar to remove the left recursion.
    – sepp2k
    Nov 11 at 4:11






  • 1




    "Can someone provide me with guidance" and "I am stuck" are not questions. However, Google did find the following for me when I searched for "antlr left recursion tutorial" : theantlrguy.atlassian.net/wiki/spaces/ANTLR3/pages/2687334/…
    – Stephen C
    Nov 11 at 6:22











  • @StephenC Note that this information is outdated. Unlike ANTLR3, ANTLR4 does support left-recursion within a single rule, so the simplest solution to OP's problem is really just to put everything into one expression rule.
    – sepp2k
    Nov 11 at 14:37














up vote
0
down vote

favorite












I'm trying to covert this BNF grammar for validating boolean expression which is currently being used in Swift. Now want to implement similar parser in Java. I came across Antlr4 lib and want to use it to generate parser for the same grammar. I'm not very familiar with antlr4. Can someone please provide me some guidance? This is what I have so far.



Expr ::= <ConcatenationExpr>;
NonInfixExpr ::= <BracketExpr>
| <Function>
| <Literal>
| <Accessor>;
BracketExpr ::= '(' <Expr> ')';
Accessor ::= ('$' <AccessorComponent> | <AccessorComponent>) ('.' <AccessorComponent> )*;
AccessorComponent ::= 'Identifier' ':' 'Identifier' | 'Identifier';
Function ::= 'Identifier' '(' ')' | ('Identifier' '(' <Expr> (',' <Expr>)* ')');
Literal ::= 'True'
| 'False'
| 'Null'
| 'Number'
| 'Text';
ConcatenationExpr ::= <AndExpr>
| <ConcatenationExpr> '&' <AndExpr>;
AndExpr ::= <OrExpr>
| <AndExpr> '&&' <OrExpr>;
OrExpr ::= <EqualityExpr>
| <OrExpr> '||' <EqualityExpr>;
EqualityExpr ::= <ComparisonExpr>
| <EqualityExpr> ('==' | '=' | '!=' | '<>') <ComparisonExpr>;
ComparisonExpr ::= <AddExpr>
| <AddExpr> ('<' | '<=' | '>' | '>=') <AddExpr>;
AddExpr ::= <ExponentialExpr>
| <AddExpr> ('+' | '-') <ExponentialExpr>;
ExponentialExpr ::= <MultExpr>
| <ExponentialExpr> '^' <MultExpr>;
MultExpr ::= <NonInfixExpr>
| <MultExpr> ('*' | '/') <NonInfixExpr>;


I tried to convert to g4 and this is what it looks like.



grammar VALIDATE;

Expr
: ConcatenationExpr ';'
;

NonInfixExpr
: BracketExpr
| Function
| Literal
| Accessor
;

BracketExpr
: '(' Expr ')'
;

Accessor
: ('$' AccessorComponent | AccessorComponent ) ('.' AccessorComponent )*
;

AccessorComponent
: 'Identifier' ':' 'Identifier' | 'Identifier'
;

Function
: 'Identifier' '(' ')' | ('Identifier' '(' Expr (',' Expr)* ')')
;

Literal
: 'True' | 'False' | 'Null' | 'Number' | 'Text'
;

ConcatenationExpr
: AndExpr
| ConcatenationExpr '&&' AndExpr
;

AndExpr
: OrExpr | AndExpr '&&' OrExpr
;

OrExpr
: EqualityExpr | OrExpr '||' EqualityExpr
;

EqualityExpr
: ComparisonExpr | EqualityExpr ('==' | '=' | '!=' | '<>') ComparisonExpr
;

ComparisonExpr
: AddExpr | AddExpr ('<' | '<=' | '>' | '>=') AddExpr
;

AddExpr
: ExponentialExpr | AddExpr ('+' | '-') ExponentialExpr
;

ExponentialExpr
: MultExpr | ExponentialExpr '^' MultExpr
;

MultExpr
: NonInfixExpr | MultExpr ('*' | '/') NonInfixExpr
;


I'm stuck at antlr4 VALIDATE.g4 step. I'm not sure I did the conversion right.



error(119): VALIDATE.g4::: The following sets of rules are mutually left-recursive [MultExpr] and [ExponentialExpr] and [AddExpr] and [EqualityExpr] and [OrExpr] and [AndExpr] and [ConcatenationExpr]
error(99): VALIDATE.g4::: grammar VALIDATE has no rules









share|improve this question





















  • ANTLR does not support left-recursion across multiple rules. You'll need to either combine all your expression rules into a single rule Expr with the alternatives sorted by precedence; or rewrite the grammar to remove the left recursion.
    – sepp2k
    Nov 11 at 4:11






  • 1




    "Can someone provide me with guidance" and "I am stuck" are not questions. However, Google did find the following for me when I searched for "antlr left recursion tutorial" : theantlrguy.atlassian.net/wiki/spaces/ANTLR3/pages/2687334/…
    – Stephen C
    Nov 11 at 6:22











  • @StephenC Note that this information is outdated. Unlike ANTLR3, ANTLR4 does support left-recursion within a single rule, so the simplest solution to OP's problem is really just to put everything into one expression rule.
    – sepp2k
    Nov 11 at 14:37












up vote
0
down vote

favorite









up vote
0
down vote

favorite











I'm trying to covert this BNF grammar for validating boolean expression which is currently being used in Swift. Now want to implement similar parser in Java. I came across Antlr4 lib and want to use it to generate parser for the same grammar. I'm not very familiar with antlr4. Can someone please provide me some guidance? This is what I have so far.



Expr ::= <ConcatenationExpr>;
NonInfixExpr ::= <BracketExpr>
| <Function>
| <Literal>
| <Accessor>;
BracketExpr ::= '(' <Expr> ')';
Accessor ::= ('$' <AccessorComponent> | <AccessorComponent>) ('.' <AccessorComponent> )*;
AccessorComponent ::= 'Identifier' ':' 'Identifier' | 'Identifier';
Function ::= 'Identifier' '(' ')' | ('Identifier' '(' <Expr> (',' <Expr>)* ')');
Literal ::= 'True'
| 'False'
| 'Null'
| 'Number'
| 'Text';
ConcatenationExpr ::= <AndExpr>
| <ConcatenationExpr> '&' <AndExpr>;
AndExpr ::= <OrExpr>
| <AndExpr> '&&' <OrExpr>;
OrExpr ::= <EqualityExpr>
| <OrExpr> '||' <EqualityExpr>;
EqualityExpr ::= <ComparisonExpr>
| <EqualityExpr> ('==' | '=' | '!=' | '<>') <ComparisonExpr>;
ComparisonExpr ::= <AddExpr>
| <AddExpr> ('<' | '<=' | '>' | '>=') <AddExpr>;
AddExpr ::= <ExponentialExpr>
| <AddExpr> ('+' | '-') <ExponentialExpr>;
ExponentialExpr ::= <MultExpr>
| <ExponentialExpr> '^' <MultExpr>;
MultExpr ::= <NonInfixExpr>
| <MultExpr> ('*' | '/') <NonInfixExpr>;


I tried to convert to g4 and this is what it looks like.



grammar VALIDATE;

Expr
: ConcatenationExpr ';'
;

NonInfixExpr
: BracketExpr
| Function
| Literal
| Accessor
;

BracketExpr
: '(' Expr ')'
;

Accessor
: ('$' AccessorComponent | AccessorComponent ) ('.' AccessorComponent )*
;

AccessorComponent
: 'Identifier' ':' 'Identifier' | 'Identifier'
;

Function
: 'Identifier' '(' ')' | ('Identifier' '(' Expr (',' Expr)* ')')
;

Literal
: 'True' | 'False' | 'Null' | 'Number' | 'Text'
;

ConcatenationExpr
: AndExpr
| ConcatenationExpr '&&' AndExpr
;

AndExpr
: OrExpr | AndExpr '&&' OrExpr
;

OrExpr
: EqualityExpr | OrExpr '||' EqualityExpr
;

EqualityExpr
: ComparisonExpr | EqualityExpr ('==' | '=' | '!=' | '<>') ComparisonExpr
;

ComparisonExpr
: AddExpr | AddExpr ('<' | '<=' | '>' | '>=') AddExpr
;

AddExpr
: ExponentialExpr | AddExpr ('+' | '-') ExponentialExpr
;

ExponentialExpr
: MultExpr | ExponentialExpr '^' MultExpr
;

MultExpr
: NonInfixExpr | MultExpr ('*' | '/') NonInfixExpr
;


I'm stuck at antlr4 VALIDATE.g4 step. I'm not sure I did the conversion right.



error(119): VALIDATE.g4::: The following sets of rules are mutually left-recursive [MultExpr] and [ExponentialExpr] and [AddExpr] and [EqualityExpr] and [OrExpr] and [AndExpr] and [ConcatenationExpr]
error(99): VALIDATE.g4::: grammar VALIDATE has no rules









share|improve this question













I'm trying to covert this BNF grammar for validating boolean expression which is currently being used in Swift. Now want to implement similar parser in Java. I came across Antlr4 lib and want to use it to generate parser for the same grammar. I'm not very familiar with antlr4. Can someone please provide me some guidance? This is what I have so far.



Expr ::= <ConcatenationExpr>;
NonInfixExpr ::= <BracketExpr>
| <Function>
| <Literal>
| <Accessor>;
BracketExpr ::= '(' <Expr> ')';
Accessor ::= ('$' <AccessorComponent> | <AccessorComponent>) ('.' <AccessorComponent> )*;
AccessorComponent ::= 'Identifier' ':' 'Identifier' | 'Identifier';
Function ::= 'Identifier' '(' ')' | ('Identifier' '(' <Expr> (',' <Expr>)* ')');
Literal ::= 'True'
| 'False'
| 'Null'
| 'Number'
| 'Text';
ConcatenationExpr ::= <AndExpr>
| <ConcatenationExpr> '&' <AndExpr>;
AndExpr ::= <OrExpr>
| <AndExpr> '&&' <OrExpr>;
OrExpr ::= <EqualityExpr>
| <OrExpr> '||' <EqualityExpr>;
EqualityExpr ::= <ComparisonExpr>
| <EqualityExpr> ('==' | '=' | '!=' | '<>') <ComparisonExpr>;
ComparisonExpr ::= <AddExpr>
| <AddExpr> ('<' | '<=' | '>' | '>=') <AddExpr>;
AddExpr ::= <ExponentialExpr>
| <AddExpr> ('+' | '-') <ExponentialExpr>;
ExponentialExpr ::= <MultExpr>
| <ExponentialExpr> '^' <MultExpr>;
MultExpr ::= <NonInfixExpr>
| <MultExpr> ('*' | '/') <NonInfixExpr>;


I tried to convert to g4 and this is what it looks like.



grammar VALIDATE;

Expr
: ConcatenationExpr ';'
;

NonInfixExpr
: BracketExpr
| Function
| Literal
| Accessor
;

BracketExpr
: '(' Expr ')'
;

Accessor
: ('$' AccessorComponent | AccessorComponent ) ('.' AccessorComponent )*
;

AccessorComponent
: 'Identifier' ':' 'Identifier' | 'Identifier'
;

Function
: 'Identifier' '(' ')' | ('Identifier' '(' Expr (',' Expr)* ')')
;

Literal
: 'True' | 'False' | 'Null' | 'Number' | 'Text'
;

ConcatenationExpr
: AndExpr
| ConcatenationExpr '&&' AndExpr
;

AndExpr
: OrExpr | AndExpr '&&' OrExpr
;

OrExpr
: EqualityExpr | OrExpr '||' EqualityExpr
;

EqualityExpr
: ComparisonExpr | EqualityExpr ('==' | '=' | '!=' | '<>') ComparisonExpr
;

ComparisonExpr
: AddExpr | AddExpr ('<' | '<=' | '>' | '>=') AddExpr
;

AddExpr
: ExponentialExpr | AddExpr ('+' | '-') ExponentialExpr
;

ExponentialExpr
: MultExpr | ExponentialExpr '^' MultExpr
;

MultExpr
: NonInfixExpr | MultExpr ('*' | '/') NonInfixExpr
;


I'm stuck at antlr4 VALIDATE.g4 step. I'm not sure I did the conversion right.



error(119): VALIDATE.g4::: The following sets of rules are mutually left-recursive [MultExpr] and [ExponentialExpr] and [AddExpr] and [EqualityExpr] and [OrExpr] and [AndExpr] and [ConcatenationExpr]
error(99): VALIDATE.g4::: grammar VALIDATE has no rules






java parsing antlr4 parser-generator






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 11 at 4:06









Abhijit Parate

1




1











  • ANTLR does not support left-recursion across multiple rules. You'll need to either combine all your expression rules into a single rule Expr with the alternatives sorted by precedence; or rewrite the grammar to remove the left recursion.
    – sepp2k
    Nov 11 at 4:11






  • 1




    "Can someone provide me with guidance" and "I am stuck" are not questions. However, Google did find the following for me when I searched for "antlr left recursion tutorial" : theantlrguy.atlassian.net/wiki/spaces/ANTLR3/pages/2687334/…
    – Stephen C
    Nov 11 at 6:22











  • @StephenC Note that this information is outdated. Unlike ANTLR3, ANTLR4 does support left-recursion within a single rule, so the simplest solution to OP's problem is really just to put everything into one expression rule.
    – sepp2k
    Nov 11 at 14:37
















  • ANTLR does not support left-recursion across multiple rules. You'll need to either combine all your expression rules into a single rule Expr with the alternatives sorted by precedence; or rewrite the grammar to remove the left recursion.
    – sepp2k
    Nov 11 at 4:11






  • 1




    "Can someone provide me with guidance" and "I am stuck" are not questions. However, Google did find the following for me when I searched for "antlr left recursion tutorial" : theantlrguy.atlassian.net/wiki/spaces/ANTLR3/pages/2687334/…
    – Stephen C
    Nov 11 at 6:22











  • @StephenC Note that this information is outdated. Unlike ANTLR3, ANTLR4 does support left-recursion within a single rule, so the simplest solution to OP's problem is really just to put everything into one expression rule.
    – sepp2k
    Nov 11 at 14:37















ANTLR does not support left-recursion across multiple rules. You'll need to either combine all your expression rules into a single rule Expr with the alternatives sorted by precedence; or rewrite the grammar to remove the left recursion.
– sepp2k
Nov 11 at 4:11




ANTLR does not support left-recursion across multiple rules. You'll need to either combine all your expression rules into a single rule Expr with the alternatives sorted by precedence; or rewrite the grammar to remove the left recursion.
– sepp2k
Nov 11 at 4:11




1




1




"Can someone provide me with guidance" and "I am stuck" are not questions. However, Google did find the following for me when I searched for "antlr left recursion tutorial" : theantlrguy.atlassian.net/wiki/spaces/ANTLR3/pages/2687334/…
– Stephen C
Nov 11 at 6:22





"Can someone provide me with guidance" and "I am stuck" are not questions. However, Google did find the following for me when I searched for "antlr left recursion tutorial" : theantlrguy.atlassian.net/wiki/spaces/ANTLR3/pages/2687334/…
– Stephen C
Nov 11 at 6:22













@StephenC Note that this information is outdated. Unlike ANTLR3, ANTLR4 does support left-recursion within a single rule, so the simplest solution to OP's problem is really just to put everything into one expression rule.
– sepp2k
Nov 11 at 14:37




@StephenC Note that this information is outdated. Unlike ANTLR3, ANTLR4 does support left-recursion within a single rule, so the simplest solution to OP's problem is really just to put everything into one expression rule.
– sepp2k
Nov 11 at 14:37

















active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













 

draft saved


draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53245751%2fconvert-a-form-of-bnf-grammar-to-g4-grammar%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes















 

draft saved


draft discarded















































 


draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53245751%2fconvert-a-form-of-bnf-grammar-to-g4-grammar%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Top Tejano songwriter Luis Silva dead of heart attack at 64

ReactJS Fetched API data displays live - need Data displayed static

Evgeni Malkin