Skip to content

Instantly share code, notes, and snippets.

@markmarch
Created September 21, 2011 21:45
Show Gist options
  • Save markmarch/1233408 to your computer and use it in GitHub Desktop.
Save markmarch/1233408 to your computer and use it in GitHub Desktop.

Revisions

  1. Xiaoming Jia revised this gist Sep 21, 2011. 1 changed file with 11 additions and 1 deletion.
    12 changes: 11 additions & 1 deletion gistfile1.java
    Original file line number Diff line number Diff line change
    @@ -3,6 +3,13 @@
    * void means do not create AST
    * / means ordered alternative
    */

    module Sample;

    header { import java.util.*;}
    body { public static final Set<String>
    KEYWORDS = new HashSet<String>(Arrays.asList("if", "else"));}

    public void program = WS statement EOF;

    void statement = IF LPAREN expr RPAREN
    @@ -35,4 +42,7 @@

    void REL_OP = ("<="/"<"/">="/">") WS; // order alternation, cause "<" is the prefix of "<="

    void ID = :ID_INTERNAL WS
    void ID = i:ID_INTERNAL WS
    &{!KEYWORDS.contains(i)};

    void ID_INTERNAL = [a-zA-Z][a-zA-Z0-9]
  2. Xiaoming Jia revised this gist Sep 21, 2011. 1 changed file with 8 additions and 2 deletions.
    10 changes: 8 additions & 2 deletions gistfile1.java
    Original file line number Diff line number Diff line change
    @@ -3,7 +3,7 @@
    * void means do not create AST
    * / means ordered alternative
    */
    public void program = statement;
    public void program = WS statement EOF;

    void statement = IF LPAREN expr RPAREN
    statement ELSE statement
    @@ -29,4 +29,10 @@
    !: negative predicate
    neither predicate will actually consume the character
    **/
    void COMMENT = "/*"(!"*"_/"*"!"/""*/"
    void COMMENT = "/*"(!"*"_/"*"!"/""*/"

    void EOF = !_;

    void REL_OP = ("<="/"<"/">="/">") WS; // order alternation, cause "<" is the prefix of "<="

    void ID = :ID_INTERNAL WS
  3. Xiaoming Jia revised this gist Sep 21, 2011. 1 changed file with 5 additions and 1 deletion.
    6 changes: 5 additions & 1 deletion gistfile1.java
    Original file line number Diff line number Diff line change
    @@ -25,4 +25,8 @@
    // whitespace
    void WS = ([ \t\n\f]/COMMENT

    void COMMENT = "/*"(!"*"_|"*"!"/""*/"
    /** &: positive predicate
    !: negative predicate
    neither predicate will actually consume the character
    **/
    void COMMENT = "/*"(!"*"_/"*"!"/""*/"
  4. Xiaoming Jia revised this gist Sep 21, 2011. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions gistfile1.java
    Original file line number Diff line number Diff line change
    @@ -23,6 +23,6 @@
    void SEMI = ";"

    // whitespace
    void WS = ([ \t\n\f]/COMMENT)
    void WS = ([ \t\n\f]/COMMENT)˚

    void COMMENT = "/*"(!"*"_| "*/"
    void COMMENT = "/*"(!"*"_|"*"!"/""*/"
  5. Xiaoming Jia revised this gist Sep 21, 2011. 1 changed file with 28 additions and 2 deletions.
    30 changes: 28 additions & 2 deletions gistfile1.java
    Original file line number Diff line number Diff line change
    @@ -1,2 +1,28 @@
    /* Syntax Analysis production*/
    public void program = statement;
    /**
    * Syntax Analysis production
    * void means do not create AST
    * / means ordered alternative
    */
    public void program = statement;

    void statement = IF LPAREN expr RPAREN
    statement ELSE statement
    / IF LPAREN expr RPAREN statement
    / SEMI;
    void expr = ID REL_OP ID;

    // simple production , are all void , and will be dropped in AST
    void IF = "if"

    void ELSE = "else"

    void LPAREN = "("

    void RPAREN = ")"

    void SEMI = ";"

    // whitespace
    void WS = ([ \t\n\f]/COMMENT)

    void COMMENT = "/*"(!"*"_| "*/"
  6. Xiaoming Jia created this gist Sep 21, 2011.
    2 changes: 2 additions & 0 deletions gistfile1.java
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,2 @@
    /* Syntax Analysis production*/
    public void program = statement;