antlr same type arithmetic expression Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern) The Ask Question Wizard is Live! Data science time! April 2019 and salary with experience Should we burninate the [wrap] tag?What are POD types in C++?What's the canonical way to check for type in Python?How to determine a Python variable's type?What are the differences between type() and isinstance()?A grammar for one variable functions in ANTLRAssignment as expression in Antlr grammarCreating multidimensional arrays from ANTLR grammarIncorrect Parsing of simple arithmetic grammar in ANTLRMutually left-recursive?Antlr: Reference to data not yet parsed

What happens to sewage if there is no river near by?

How do I mention the quality of my school without bragging

Proof involving the spectral radius and Jordan Canonical form

Why is "Captain Marvel" translated as male in Portugal?

How can I make names more distinctive without making them longer?

Why did the IBM 650 use bi-quinary?

What are the pros and cons of Aerospike nosecones?

Stars Make Stars

What's the purpose of writing one's academic bio in 3rd person?

Does surprise arrest existing movement?

What is the longest distance a 13th-level monk can jump while attacking on the same turn?

What is a Meta algorithm?

Why is black pepper both grey and black?

Why are there no cargo aircraft with "flying wing" design?

When to stop saving and start investing?

How can players work together to take actions that are otherwise impossible?

Is it true to say that an hosting provider's DNS server is what links the entire hosting environment to ICANN?

Do you forfeit tax refunds/credits if you aren't required to and don't file by April 15?

I am not a queen, who am I?

Is above average number of years spent on PhD considered a red flag in future academia or industry positions?

What causes the vertical darker bands in my photo?

What LEGO pieces have "real-world" functionality?

How much radiation do nuclear physics experiments expose researchers to nowadays?

3 doors, three guards, one stone



antlr same type arithmetic expression



Announcing the arrival of Valued Associate #679: Cesar Manara
Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern)
The Ask Question Wizard is Live!
Data science time! April 2019 and salary with experience
Should we burninate the [wrap] tag?What are POD types in C++?What's the canonical way to check for type in Python?How to determine a Python variable's type?What are the differences between type() and isinstance()?A grammar for one variable functions in ANTLRAssignment as expression in Antlr grammarCreating multidimensional arrays from ANTLR grammarIncorrect Parsing of simple arithmetic grammar in ANTLRMutually left-recursive?Antlr: Reference to data not yet parsed



.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








1















Trying to write a antlr grammar that parse the arithmetic expression for only same typed variable. If it is not a same type as left or right side, it should not be parse. This is what I have;



stat
: Left = VARIABLE Op = ASSIGMENT Right = expr # Assigment
;

expr
: '(' Exp = expr ')' # Parens
| MINUS Exp = expr # UnaryMinus
| Left = expr Op = (TIMES | DIV) Right = expr # MulDiv
| Left = expr Op = (PLUS | MINUS) Right = expr # AddSub
| (VARIABLE | CONSTANT) # Element
;

ASSIGMENT : '=' ;
PLUS : '+' ;
MINUS : '-' ;
TIMES : '*' ;
DIV : '/' ;
LPAREN : '(' ;
RPAREN : ')' ;


I don't want anything like x = 5 + 'f' or x = c - 5, (if c is variable that is not integer)










share|improve this question

















  • 4





    You can't enforce that kind of constraint in the grammar. You'd check these things in the type checker, not the parser.

    – sepp2k
    Mar 8 at 19:42

















1















Trying to write a antlr grammar that parse the arithmetic expression for only same typed variable. If it is not a same type as left or right side, it should not be parse. This is what I have;



stat
: Left = VARIABLE Op = ASSIGMENT Right = expr # Assigment
;

expr
: '(' Exp = expr ')' # Parens
| MINUS Exp = expr # UnaryMinus
| Left = expr Op = (TIMES | DIV) Right = expr # MulDiv
| Left = expr Op = (PLUS | MINUS) Right = expr # AddSub
| (VARIABLE | CONSTANT) # Element
;

ASSIGMENT : '=' ;
PLUS : '+' ;
MINUS : '-' ;
TIMES : '*' ;
DIV : '/' ;
LPAREN : '(' ;
RPAREN : ')' ;


I don't want anything like x = 5 + 'f' or x = c - 5, (if c is variable that is not integer)










share|improve this question

















  • 4





    You can't enforce that kind of constraint in the grammar. You'd check these things in the type checker, not the parser.

    – sepp2k
    Mar 8 at 19:42













1












1








1








Trying to write a antlr grammar that parse the arithmetic expression for only same typed variable. If it is not a same type as left or right side, it should not be parse. This is what I have;



stat
: Left = VARIABLE Op = ASSIGMENT Right = expr # Assigment
;

expr
: '(' Exp = expr ')' # Parens
| MINUS Exp = expr # UnaryMinus
| Left = expr Op = (TIMES | DIV) Right = expr # MulDiv
| Left = expr Op = (PLUS | MINUS) Right = expr # AddSub
| (VARIABLE | CONSTANT) # Element
;

ASSIGMENT : '=' ;
PLUS : '+' ;
MINUS : '-' ;
TIMES : '*' ;
DIV : '/' ;
LPAREN : '(' ;
RPAREN : ')' ;


I don't want anything like x = 5 + 'f' or x = c - 5, (if c is variable that is not integer)










share|improve this question














Trying to write a antlr grammar that parse the arithmetic expression for only same typed variable. If it is not a same type as left or right side, it should not be parse. This is what I have;



stat
: Left = VARIABLE Op = ASSIGMENT Right = expr # Assigment
;

expr
: '(' Exp = expr ')' # Parens
| MINUS Exp = expr # UnaryMinus
| Left = expr Op = (TIMES | DIV) Right = expr # MulDiv
| Left = expr Op = (PLUS | MINUS) Right = expr # AddSub
| (VARIABLE | CONSTANT) # Element
;

ASSIGMENT : '=' ;
PLUS : '+' ;
MINUS : '-' ;
TIMES : '*' ;
DIV : '/' ;
LPAREN : '(' ;
RPAREN : ')' ;


I don't want anything like x = 5 + 'f' or x = c - 5, (if c is variable that is not integer)







math types expression antlr






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Mar 8 at 16:15









Kevin SKevin S

171




171







  • 4





    You can't enforce that kind of constraint in the grammar. You'd check these things in the type checker, not the parser.

    – sepp2k
    Mar 8 at 19:42












  • 4





    You can't enforce that kind of constraint in the grammar. You'd check these things in the type checker, not the parser.

    – sepp2k
    Mar 8 at 19:42







4




4





You can't enforce that kind of constraint in the grammar. You'd check these things in the type checker, not the parser.

– sepp2k
Mar 8 at 19:42





You can't enforce that kind of constraint in the grammar. You'd check these things in the type checker, not the parser.

– sepp2k
Mar 8 at 19:42












1 Answer
1






active

oldest

votes


















1














It's called Semantic analysis.



When parsing is done you have to walk through the generated AST and check correctness of each expression and variable.






share|improve this answer























    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',
    autoActivateHeartbeat: false,
    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%2f55067020%2fantlr-same-type-arithmetic-expression%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    It's called Semantic analysis.



    When parsing is done you have to walk through the generated AST and check correctness of each expression and variable.






    share|improve this answer



























      1














      It's called Semantic analysis.



      When parsing is done you have to walk through the generated AST and check correctness of each expression and variable.






      share|improve this answer

























        1












        1








        1







        It's called Semantic analysis.



        When parsing is done you have to walk through the generated AST and check correctness of each expression and variable.






        share|improve this answer













        It's called Semantic analysis.



        When parsing is done you have to walk through the generated AST and check correctness of each expression and variable.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Mar 11 at 12:10









        Pavel SmirnovPavel Smirnov

        2,392818




        2,392818





























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55067020%2fantlr-same-type-arithmetic-expression%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

            1928 у кіно

            Захаров Федір Захарович

            Ель Греко