编写简单语言的解析器 [英] writing parser of a simple language

查看:70
本文介绍了编写简单语言的解析器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试设计一种简单的语言,类似于嘴唇,架构.我已经写了它的词法分析器(tokenizer).我可以分为运算符,标识符等.但是我现在尝试编写解析器.为此,仅举一个例子就足够了. 有人可以给我一个用Java代码举例的例子吗?此外,每个人都提到了使用antlr的用法.尝试使用antlr的人可以在这个问题中使用我的示例显示示例吗?

I'm trying to design a simple language is similar to lips, schema. I have written its lexer(tokenizer). I can seperate into operators, identifiers etc. But I try now writing parser. For this, just one example is enough for me. Can someone give me an example in java code just for the example? Besides, everyone mentions of usage antlr. Can someone who attempts to use antlr show an example using my example in this question?

  • EXP -> EXPI | EXPB
  • EXPI -> (+ EXPI EXPI) | (- EXPI EXPI) | (* EXPI EXPI) | (/ EXPI EXPI) | Id | IntegerValue | (Id EXPLIST)
  • EXPB -> (and EXPB EXPB)
  • EXP -> EXPI | EXPB
  • EXPI -> (+ EXPI EXPI) | (- EXPI EXPI) | (* EXPI EXPI) | (/ EXPI EXPI) | Id | IntegerValue | (Id EXPLIST)
  • EXPB -> (and EXPB EXPB)

如果我的输入是(+ 1 2),我希望得到输出

if my input is (+ 1 2), I hope to get as output

 - START-> INPUT
 - -> EXP
 - -> EXPI
 - -> (+ EXPI EXPI)
 - -> (+ EXPI Id)
 - -> (+ Id Id)

关于LR(移位减少解析).我有一个简单的示例,我不知道是否可以修改代码以进行解析.但是,有一点引起了我的注意,堆栈使用情况适合LR算法.不是吗?

It is about LR(shift-reduce parsing). I have a simple example that I don't know whether I can modify the code or not for the parsing. But, a point catches my attention, stack usage is proper for LR algorithm. Doesn't it ?

import java.util.Stack;

public class SmallLisp {

    Stack<String> stack;

    public SmallLisp(){
        String[] tokens = new String[]{"(","+","2","(","+","3","2",")",")",};
        stack = new Stack<String>();
        for (int i=0;i<tokens.length;i++){
            stack.push(tokens[i]);
            if(tokens[i].equals(")")) Interprete();
        }
    }

    public void Interprete(){
        String tok;
        Stack<String> callStack = new Stack<String>();
        tok = stack.pop(); /* This is the ) character */
        while(!(tok=stack.pop()).equals("(")){
            callStack.push(tok);
        }
        Call(callStack);
    }

    public void Call(Stack<String> callStack){
        String func = callStack.pop(); /* This is the operator or function */
        if(func.equals("+")) {
            double result = Plus(callStack);
            stack.push(String.valueOf(result));
        }
        //if(func.equals("-")) Minus(callStack);
    }

    public double Plus(Stack<String> callStack){
        double a = Double.parseDouble(callStack.pop());
        double b = Double.parseDouble(callStack.pop());
        System.out.println("Answer is "+(a+b));
        return(a+b);
    }

    public static void main(String[] args) {
        new SmallLisp();
    }
}

推荐答案

如果要避免编写自己的解析器,可以使用

If you want to avoid to write your own Parser you could use ANTLR

这是一个解析器生成器,如果您使用有效的语法支持它,那么它将为您完成所有工作.

This is a parser generator that does all the work for you if you support it with a valid grammar.

如果您仍然想自己编写它,则仍然可以使用ANTLR来查看他们如何创建解析器/词法分析器.

If you still want to write it yourself you could still use ANTLR to see how they create their parser/lexers.

这篇关于编写简单语言的解析器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆