tinyc.g
上传用户:afrynkmhm
上传日期:2007-01-06
资源大小:1262k
文件大小:1k
- /*
- * Make sure to run antlr.Tool on the lexer.g file first!
- */
- options {
- mangleLiteralPrefix = "TK_";
- }
- {
- import java.io.*;
- class Main {
- public static void main(String[] args) {
- try {
- TinyCLexer lexer = new TinyCLexer(new DataInputStream(System.in));
- TinyCParser parser = new TinyCParser(lexer);
- parser.program();
- } catch(Exception e) {
- System.err.println("exception: "+e);
- }
- }
- }
- }
- class TinyCParser extends Parser;
- options {
- importVocab=TinyC; // use vocab generated by lexer
- }
- program
- : ( declaration )* EOF
- ;
- declaration
- : (variable) => variable
- | function
- ;
- declarator
- : id:ID
- | STAR id2:ID
- ;
- variable
- : type declarator SEMI
- ;
- function
- : type id:ID LPAREN
- (formalParameter (COMMA formalParameter)*)?
- RPAREN
- block
- ;
- formalParameter
- : type declarator
- ;
- type:
- (
- TK_int
- | TK_char
- | id:ID
- )
- ;
- block
- : LCURLY ( statement )* RCURLY
- ;
- statement
- : (declaration) => declaration
- | expr SEMI
- | TK_if LPAREN expr RPAREN statement
- ( TK_else statement )?
- | TK_while LPAREN expr RPAREN statement
- | block
- ;
- expr: assignExpr
- ;
- assignExpr
- : aexpr (ASSIGN assignExpr)?
- ;
- aexpr
- : mexpr (PLUS mexpr)*
- ;
- mexpr
- : atom (STAR atom)*
- ;
- atom: ID
- | INT
- | CHAR_LITERAL
- | STRING_LITERAL
- ;