示例#1
0
 public Parser(Scanner s)
 {
     scanner = s;
     nil     = new Nil();
     tr      = new BoolLit(true);
     fa      = new BoolLit(false);
 }
        //parses grammar for exp ->
        public Node parseExp()
        {
            Token tok;

            tok = scanner.getNextToken();
            //checks for end of file
            if (tok == null)
            {
                Nil retNil = nilPoint;
                return(retNil);
            }
            //checks for case of left parenthesis
            if (tok.getType() == TokenType.LPAREN)
            {
                return(parseRest(true));
            }
            //checks for case of false constant
            else if (tok.getType() == TokenType.FALSE)
            {
                BoolLit ret = fBool;
                return(ret);
            }
            //checks for case of true constant
            else if (tok.getType() == TokenType.TRUE)
            {
                BoolLit ret = tBool;
                return(ret);
            }
            //checks for case of QUOTE symbol
            else if (tok.getType() == TokenType.QUOTE)
            {
                return(new Cons(new Ident("quote"), new Cons(parseExp(), new Nil()), true));
            }
            //checks for case int constant
            else if (tok.getType() == TokenType.INT)
            {
                return(new IntLit(tok.getIntVal()));
            }
            //checks for case of string constant
            else if (tok.getType() == TokenType.STRING)
            {
                return(new StringLit(tok.getStringVal()));
            }
            //checks for case of identifier
            else if (tok.getType() == TokenType.IDENT)
            {
                return(new Ident(tok.getName()));
            }

            return(null);
        }