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

Is it possible to understand, when does semantic predicates work and they doesn't? #4780

Open
dims12 opened this issue Feb 22, 2025 · 0 comments

Comments

@dims12
Copy link

dims12 commented Feb 22, 2025

Below are two grammars.

In this grammar, semantic predicates do "work". I.e. if they are false, rules don't match and if they are true, rules do match:

parser grammar MyParser;

options {
    tokenVocab=MyLexer;
}

expr
    : term
    | expr asterisk expr (asterisk expr)*
    | expr plus expr (plus expr)*
    ;

plus: {_input.LT(1).getText().equals("+")}? SPECID;
asterisk: {_input.LT(1).getText().equals("*")}? SPECID;


term
    : ALNUMID
    | STRID
    | LPAREN expr RPAREN
    ;

and in this grammar, semantic predicates don't work. I.e. if they are false, rules still match bu error is printed.

expr
    : add
    | mult
    | term
    ;


mult
    : term (asterisk) term ((asterisk) term)*
    ;

add
    : (term|mult) plus (term|mult) (plus (term|mult))*
    ;


plus: {_input.LT(1).getText().equals("+")}? SPECID;
asterisk: {_input.LT(1).getText().equals("*")}? SPECID;



term
    : ALNUMID
    | STRID
    | LPAREN expr RPAREN
    ;

In both cases I am trying to run simple tests to parse 2 + 2 and 2 * 2.

Is it possible to understand beforehand, will definition work or not? What is the logic here?

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

1 participant