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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T00:34:49+00:00 2026-05-22T00:34:49+00:00

Im trying to create a parser which sould transalte english sentences into drawn shapes

  • 0

Im trying to create a parser which sould transalte english sentences into drawn shapes on a canvas. For example:
“Create a red box” should create a box on the canvas which is red.

I came up with this grammar file from a tutorial at your wiki. Dont know if it is correct, would be nice if someone could check it =)

grammar Shrdlu;

tokens {

    //operational tokens
    MOVE        = 'move';
    TRANSFORM   = 'transform';
    CREATE      = 'create';
    MAKE        = 'make';
    ADD         = 'add';
    REMOVE      = 'remove';
    DELETE      = 'delete';

    //shape tokens
    BOX         = 'box';
    RECTANGLE   = 'rectangel';
    CIRCLE      = 'circle';
    TRIANGLE    = 'triangle';
    SHAPE       = 'shape';
    SQUARE      = 'square';

    //color tokens
    RED         = 'red';
    BLUE        = 'blue';
    GREEN       = 'green';
    BLACK       = 'black';
    PURPLE      = 'purple';
    YELLOW      = 'yellow';
    ORANGE      = 'orange';
    PINK        = 'pink';

    //size tokens
    BIG         = 'big';
    LARGE       = 'large';
    TALL        = 'tall';
    SMALL       = 'small';
    TINY        = 'tiny';
    SHORT       = 'short';

    //relation size
    BIGGEST     = 'biggest';
    LARGEST     = 'largest';
    TALLEST     = 'tallest';
    SMALLEST    = 'smallest';   
    SHORTEST    = 'shortest';

    //argument size
    BIGGER      = 'bigger';
    SMALLER     = 'smaller';
    SHORTER     = 'shorter';
    TALLER      = 'taller';
    LARGER      = 'larger';

    //alignment tokens
    LEFT        = 'left';
    RIGHT       = 'right';
    OVER        = 'over';
    UNDER       = 'under';
    ABOVE       = 'above';
    BELOW       = 'below';
    TOP         = 'top';
    BOTTOM      = 'bottom';

    //prefix tokens
    A           = 'a';
    AN          = 'an';
    ALL         = 'all';
    ANY         = 'any';
    EACH        = 'each';
    THE         = 'the';

}

/*------------------------------------------------------------------
 * PARSER RULES
 *------------------------------------------------------------------*/

command
    :   sentence EOF
    |
    ;

sentence
    :   WS? action WS object (WS argument)? WS?
    ;

action

    :   MOVE
    |   TRANSFORM
    |   CREATE
    |   MAKE
    |   ADD
    |   REMOVE
    |   DELETE
    ;

object
    :   prefix WS (property WS)?  shape (WS relation WS object)?
    ;

a    rgument
    :   color
    |   sizearg
    |   alignment
    ;

prefix
    :   A
    |   AN
    |   ALL
    |   ANY
    |   EACH
    |   THE
    ;

shape
    :   BOX
    |   RECTANGLE
    |   CIRCLE
    |   TRIANGLE
    |   SHAPE
    |   SQUARE
    ;

property
    :   size (WS property)?
    |   color (WS property)?
    ;


size
    :   BIG
    |   LARGE
    |   TALL
    |   SMALL
    |   TINY
    |   SHORT
    ;

sizearg
    :   BIGGER
    |   LARGER
    |   SMALLER
    |   TALLER
    |   SHORTER 
    ;

relsize
    :   BIGGEST
    |   SMALLEST
    |   TALLEST
    |   LARGEST
    ;   


relation
    :   alignment
    |   relsize
    ;

color
    :   RED
    |   BLUE
    |   GREEN
    |   BLACK
    |   PURPLE
    |   YELLOW
    |   ORANGE
    |   PINK
    ;

alignment
    :   LEFT
    |   RIGHT
    |   OVER
    |   UNDER
    |   ABOVE
    |   BELOW
    |   TOP
    |   BOTTOM
    ;

/*------------------------------------------------------------------
 * LEXER RULES
 *------------------------------------------------------------------*/

NEWLINE
    :   '\r'? '\n'
    ;

WS
    :   (' '|'\t'|'\n'|'\r')+ {skip();}
    ;

Then i used this code to generate the lexer and parser. What is my next step. How do i use the parser to, for example transalte “create” into the creation of an object. Could someone point me in the right direction?

  • 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-22T00:34:50+00:00Added an answer on May 22, 2026 at 12:34 am

    Because of {skip();} in the WS rule, your lexer will never create any WS tokens. So parser rules that have WS in them, like the sentence rule, will never match. So in that sense, you grammar is wrong.

    Richard wrote:

    into the creation of an object.

    EDIT

    You can give your parser custom attributes, like a Graphics object you’re going to paint on (see the @parser::members { ... } section of the grammar below). And embed custom code inside your grammar rules between { and }. As a demo I only added some System.out.println‘s, but you should replace those with your actual painting, of course. I also removed the WS tokens from your parser rules (also note that Create does not equal create!).

    The modified grammar:

    grammar Shrdlu;
    
    tokens {
    
        //operational tokens
        MOVE        = 'move';
        TRANSFORM   = 'transform';
        CREATE      = 'create';
        MAKE        = 'make';
        ADD         = 'add';
        REMOVE      = 'remove';
        DELETE      = 'delete';
    
        //shape tokens
        BOX         = 'box';
        RECTANGLE   = 'rectangel';
        CIRCLE      = 'circle';
        TRIANGLE    = 'triangle';
        SHAPE       = 'shape';
        SQUARE      = 'square';
    
        //color tokens
        RED         = 'red';
        BLUE        = 'blue';
        GREEN       = 'green';
        BLACK       = 'black';
        PURPLE      = 'purple';
        YELLOW      = 'yellow';
        ORANGE      = 'orange';
        PINK        = 'pink';
    
        //size tokens
        BIG         = 'big';
        LARGE       = 'large';
        TALL        = 'tall';
        SMALL       = 'small';
        TINY        = 'tiny';
        SHORT       = 'short';
    
        //relation size
        BIGGEST     = 'biggest';
        LARGEST     = 'largest';
        TALLEST     = 'tallest';
        SMALLEST    = 'smallest';   
        SHORTEST    = 'shortest';
    
        //argument size
        BIGGER      = 'bigger';
        SMALLER     = 'smaller';
        SHORTER     = 'shorter';
        TALLER      = 'taller';
        LARGER      = 'larger';
    
        //alignment tokens
        LEFT        = 'left';
        RIGHT       = 'right';
        OVER        = 'over';
        UNDER       = 'under';
        ABOVE       = 'above';
        BELOW       = 'below';
        TOP         = 'top';
        BOTTOM      = 'bottom';
    
        //prefix tokens
        A           = 'a';
        AN          = 'an';
        ALL         = 'all';
        ANY         = 'any';
        EACH        = 'each';
        THE         = 'the';
    
    }
    
    @parser::members {
    
      private java.awt.Graphics graphics;
    
      public ShrdluParser(TokenStream tokens, java.awt.Graphics g) {
        super(tokens);
        graphics = g;
      }
    }
    
    /*------------------------------------------------------------------
     * PARSER RULES
     *------------------------------------------------------------------*/
    
    command
        :   sentence EOF
        |
        ;
    
    sentence
        :   action object argument?
        ;
    
    action
    
        :   MOVE
        |   TRANSFORM
        |   CREATE    {System.out.println("I should create a ...");}
        |   MAKE
        |   ADD
        |   REMOVE
        |   DELETE
        ;
    
    object
        :   prefix property?  shape (relation object)?
        ;
    
    argument
        :   color
        |   sizearg
        |   alignment
        ;
    
    prefix
        :   A
        |   AN
        |   ALL
        |   ANY
        |   EACH
        |   THE
        ;
    
    shape
        :   BOX       {System.out.println("box ...");}
        |   RECTANGLE
        |   CIRCLE
        |   TRIANGLE
        |   SHAPE
        |   SQUARE
        ;
    
    property
        :   size property?
        |   color property?
        ;
    
    
    size
        :   BIG
        |   LARGE
        |   TALL
        |   SMALL
        |   TINY
        |   SHORT
        ;
    
    sizearg
        :   BIGGER
        |   LARGER
        |   SMALLER
        |   TALLER
        |   SHORTER 
        ;
    
    relsize
        :   BIGGEST
        |   SMALLEST
        |   TALLEST
        |   LARGEST
        ;   
    
    
    relation
        :   alignment
        |   relsize
        ;
    
    color
        :   RED   {System.out.println("red ...");}
        |   BLUE
        |   GREEN
        |   BLACK
        |   PURPLE
        |   YELLOW
        |   ORANGE
        |   PINK
        ;
    
    alignment
        :   LEFT
        |   RIGHT
        |   OVER
        |   UNDER
        |   ABOVE
        |   BELOW
        |   TOP
        |   BOTTOM
        ;
    
    NEWLINE
        :   '\r'? '\n'
        ;
    
    WS
        :   (' '|'\t'|'\n'|'\r')+ {skip();}
        ;
    

    which can be tested with the class:

    import org.antlr.runtime.*;
    
    public class Main {
        public static void main(String[] args) throws Exception {
            ANTLRStringStream in = new ANTLRStringStream("create a red box");
            ShrdluLexer lexer = new ShrdluLexer(in);
            CommonTokenStream tokens = new CommonTokenStream(lexer);
            java.awt.Graphics g = null;
            ShrdluParser parser = new ShrdluParser(tokens, g);
            parser.command();
        }
    }
    

    Create the lexer and parser from your grammar:

    java -cp antlr-3.3.jar org.antlr.Tool Shrdlu.g
    

    then compile all .java source files:

    javac -cp antlr-3.3.jar *.java
    

    and run the Main class:

    # *nix
    java -cp .:antlr-3.3.jar Main
    
    # Windows
    java -cp .;antlr-3.3.jar Main
    

    which produces the following output:

    I should create a ...
    red ...
    box ...
    

    And one final thing, you can probably remove the NEWLINE rule: your WS rule catches such chars (and ignores them).

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

Sidebar

Related Questions

I am trying to create a generic formatter/parser combination. Example scenario: I have a
I'm trying to create a simple parser and a small text file which follows
I'm trying create a bot which automatically likes Facebook posts. Using Mechanize I can
Trying to create a macro which can be used for print debug messages when
Trying to create a PhoneGap app using the canvas tag on an iPad. The
I am trying to create an iPhone application which in some point connects to
I am trying to create a grammar which accepts any character or number or
I'm trying to create a UILabel which will inform the user of what is
I am trying create a delegate representation of constructor by emitting a Dynamic Method,
Ok so I am trying create a login script, here I am using PHP5

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.