Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7756575
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T12:51:33+00:00 2026-06-01T12:51:33+00:00

I have written grammar for a language (sample code below) //this is a procedure

  • 0

I have written grammar for a language (sample code below)

//this is a procedure
procedure main()
    $i := 0
begin
    if ($i = 0)
        $i := 10
        loop while ($i != 0)
            print($i)
            $i := $i - 1
        end loop
    end if
end procedure

The tree grammar for which is

@members {
  private Map<String, Integer> variablesTable = new HashMap<String, Integer>();
}
procedure
    :   ^('procedure' IDENTIFIER assignment_expression* 'begin' statement+)
    ;
statement
    :   assignment_expression | selection_statement
    ;
assignment_expression
    :   ^(':=' IDENTIFIER e=expression)
        { variablesTable.put($IDENTIFIER.text, e); }
    ;
expression returns [int result]
    :   ^('+' op1=expression op2=expression) { result = op1 + op2; }
    /* removed similar lines */ 
    |   IDENTIFIER { result = variablesTable.get($IDENTIFIER.text); }
    |   INTEGER { result = Integer.parseInt($INTEGER.text); }
    ;

equality_expression returns [boolean truth]
    :   ^('=' op1=expression op2=expression) { truth = op1 == op2; }
    |   ^('!=' op1=expression op2=expression) { truth = op1 != op2; }
    ;
selection_statement
    :   ^('if' e=equality_expression block1=statement+ ('else' block2=statement+)? )
        { 
            if (e)  {
                 //What do I put here?
            }
        }
    ;

While I can evaluate expressions, print messages etc., I am not able figure out how to change execution (interpretation) of program.
Like in above code, for if-else, how can I execute if-block in case predicate is true, and skip to else (if it is there)?
Same goes for loop, how can I evaluate loop-predicate at the end of loop?

Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T12:51:34+00:00Added an answer on June 1, 2026 at 12:51 pm

    Embedding code inside the tree grammar is only suitable for very simple languages (calculators and such). Embedding it will (IMO) result in an incomprehensible mess of mixed grammar rules, programming logic and rule-parameters and rule-return values. Pretty much every rule will then need at least a boolean value keeping track if the (sub) rules of it needed to be executed or not (is it inside the if– or else block?).

    When interpreting a language consisting of if– and while-statements, simple as it may be, I recommend creating custom trees/nodes and evaluate those.

    I’ve written a blog series that shows how to do just that: http://bkiers.blogspot.com/2011/03/creating-your-own-programming-language.html

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have written this grammar: expr : multExpr ( ('+' | '-') multExpr )*;
Have written all the code in a silverlight class library (dll) and linked this
I have a huge grammar for xml parsing but written a while back in
I have written some code in my VB.NET application to send an HTML e-mail
Have written the following code: m_selectCategoryTableWidget = new QTableWidget; m_selectCategoryTableWidget->setRowCount(0); m_selectCategoryTableWidget->setColumnCount(2); m_selectCategoryTableWidget->setHorizontalHeaderLabels(QStringList()<<tr(Category)<<tr(Number of items));
I have written a generic class class MyClass : MyClass<string> { } //this line
So I have this grammar: expr(op(T,B,E)) => [term(T), binop(B), expr(E)]. expr(T) => [term(T)]. term(N)
I have written a code which can reduce the grammatical boundaries for a text,
I have written the following code as, Dim report As New ReportDocument report.PrintOptions.PrinterName =
Hopefully this is a really quick one ;) I have written a lexer /

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.