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 3875920
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T22:19:21+00:00 2026-05-19T22:19:21+00:00

I’m making a static analyzer for C. I have done the lexer and parser

  • 0

I’m making a static analyzer for C.
I have done the lexer and parser using ANTLR in which generates Java code.

Does ANTLR build the AST for us automatically by options {output=AST;}? Or do I have to make the tree myself? If it does, then how to spit out the nodes on that AST?

I am currently thinking that the nodes on that AST will be used for making SSA, followed by data flow analysis in order to make the static analyzer. Am I on the right path?

  • 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-05-19T22:19:22+00:00Added an answer on May 19, 2026 at 10:19 pm

    Raphael wrote:

    Does antlr build the AST for us automatically by option{output=AST;}? Or do I have to make the tree myself? If it does, then how to spit out the nodes on that AST?

    No, the parser does not know what you want as root and as leaves for each parser rule, so you’ll have to do a bit more than just put options { output=AST; } in your grammar.

    For example, when parsing the source "true && (false || true && (true || false))" using the parser generated from the grammar:

    grammar ASTDemo;
    
    options { 
      output=AST; 
    }
    
    parse
      :  orExp
      ;
    
    orExp
      :  andExp ('||' andExp)*
      ;
    
    andExp
      :  atom ('&&' atom)*
      ;
    
    atom
      :  'true'
      |  'false'
      |  '(' orExp ')'
      ;
    
    // ignore white space characters
    Space
      :  (' ' | '\t' | '\r' | '\n') {$channel=HIDDEN;}
      ;
    

    the following parse tree is generated:

    enter image description here

    (i.e. just a flat, 1 dimensional list of tokens)

    You’ll want to tell ANTLR which tokens in your grammar become root, leaves, or simply left out of the tree.

    Creating AST’s can be done in two ways:

    1. use rewrite rules which look like this: foo : A B C D -> ^(D A B);, where foo is a parser rule that matches the tokens A B C D. So everything after the -> is the actual rewrite rule. As you can see, the token C is not used in the rewrite rule, which means it is omitted from the AST. The token placed directly after the ^( will become the root of the tree;
    2. use the tree-operators ^ and ! after a token inside your parser rules where ^ will make a token the root, and ! will delete a token from the tree. The equivalent for foo : A B C D -> ^(D A B); would be foo : A B C! D^;

    Both foo : A B C D -> ^(D A B); and foo : A B C! D^; will produce the following AST:

    enter image description here

    Now, you could rewrite the grammar as follows:

    grammar ASTDemo;
    
    options { 
      output=AST; 
    }
    
    parse
      :  orExp
      ;
    
    orExp
      :  andExp ('||'^ andExp)* // Make `||` root
      ;
    
    andExp
      :  atom ('&&'^ atom)* // Make `&&` root
      ;
    
    atom
      :  'true'
      |  'false'
      |  '(' orExp ')' -> orExp // Just a single token, no need to do `^(...)`, 
                                // we're removing the parenthesis. Note that
                                // `'('! orExp ')'!` will do exactly the same.
      ;
    
    // ignore white space characters
    Space
      :  (' ' | '\t' | '\r' | '\n') {$channel=HIDDEN;}
      ;
    

    which will create the following AST from the source "true && (false || true && (true || false))":

    enter image description here

    Related ANTLR wiki links:

    • Tree construction
    • Tree parsing
    • Tree construction facilities

    Raphael wrote:

    I am currently thinking that the nodes on that AST will be used for making SSA, followed by data flow analysis in order to make the static analyzer. Am I on the right path?

    Never did anything like that, but IMO the first thing you’d want is an AST from the source, so yeah, I guess your on the right path! 🙂

    EDIT

    Here’s how you can use the generated lexer and parser:

    import org.antlr.runtime.*;
    import org.antlr.runtime.tree.*;
    import org.antlr.stringtemplate.*;
    
    public class Main {
      public static void main(String[] args) throws Exception {
        String src = "true && (false || true && (true || false))";
        ASTDemoLexer lexer = new ASTDemoLexer(new ANTLRStringStream(src));
        ASTDemoParser parser = new ASTDemoParser(new CommonTokenStream(lexer));
        CommonTree tree = (CommonTree)parser.parse().getTree();
        DOTTreeGenerator gen = new DOTTreeGenerator();
        StringTemplate st = gen.toDOT(tree);
        System.out.println(st);
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I'm making a simple page using Google Maps API 3. My first. One marker
link Im having trouble converting the html entites into html characters, (&# 8217;) i
Does anyone know how can I replace this 2 symbol below from the string
this is what i have right now Drawing an RSS feed into the php,
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I have a French site that I want to parse, but am running into
We're building an app, our first using Rails 3, and we're having to build

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.