当前位置:   article > 正文

一个简单的java递归下降语法分析器例子

一个简单的java递归下降语法分析器例子
import parser.Parser;
import parser.RecursiveDescentParser;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class Main {
    public static void main(String[] args) {
        // 关键词
        List<String> keyList = new ArrayList<>(Arrays.asList("int","String"));
        // 关键词数
        List<Integer> keyNum = new ArrayList<>(Arrays.asList(1,2));
        // 运算符和界符
        List<String> symbolList = new ArrayList<>(Arrays.asList("+","-"));
        // 运算符和界符的数
        List<Integer> symbolNum = new ArrayList<>(Arrays.asList(10,11));

        // 从文件取出的字符
        String letter;
        // 将字符转为单词
        String words;

        String test ="int main() { int i,j; String a,b;} 123 ccd";

//        Parser parser = new Parser();
//        parser.analysis(test,keyList);

        // 测试语法分析,递归下降分析
        String input = "5+10*2";
        RecursiveDescentParser recursiveDescentParser = new RecursiveDescentParser(input);
        int rs = recursiveDescentParser.parseExpression();
        System.out.println("Result: " + rs);
    }
}

package parser;

/**
 * 递归下降解析
 * 文法规则:
 * expression = term + expression|term
 * term = = factor * term|factor
 * factor = (expression)|number
 * number = [0-9]+
 *
 */
public class RecursiveDescentParser {
    private String input;
    private int position;
    public RecursiveDescentParser(String input){
        this.input = input;
        this.position = 0;
    }

    public int parseExpression(){
        int term = parseTerm();
        if (position < input.length() && Character.toString(input.charAt(position)).equals("+")) {
            position++;
            int expression =parseExpression();
            return term+expression;
        }
        return term;
    }

    public int parseTerm(){
        int factor = parsefactor();
        if (position < input.length() && Character.toString(input.charAt(position)).equals("*")) {
            position++;
            int term =parseTerm();
            return factor*term;
        }
        return factor;
    }

    public int parsefactor(){
        if (position < input.length() && Character.toString(input.charAt(position)).equals("(")){
            position++;
            int expression =parseExpression();
            if (position < input.length() && Character.toString(input.charAt(position)).equals(")")){
                position++;
                return expression;
            }
        }else{
            StringBuilder number = new StringBuilder();
            while (position < input.length() && Character.isDigit(input.charAt(position))){
                number.append(input.charAt(position));
                position++;

                System.out.println("number: " + number);
            }
            return Integer.parseInt(number.toString());
        }

        throw new RuntimeException("错误输入");
    }
}

测试结果

如果看不懂,可以先学习一下上下文无关文法、产生式、终结符、非终结符的概念。

上下文无关文法、产生式、终结符、非终结符-CSDN博客

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/繁依Fanyi0/article/detail/470484
推荐阅读
相关标签
  

闽ICP备14008679号