parsing - 正确设置 Cup/JLex 解析

标签 parsing jflex cup

我这里有一个非常基本的词法分析器:

import java_cup.runtime.*;
import java.io.IOException;

%%

%class AnalyzerLex

%function next_token
%type java_cup.runtime.Symbol

%unicode
//%line
//%column

// %public
%final
// %abstract

%cupsym sym
%cup
%cupdebug

%eofval{
  return sym(sym.EOF);
%eofval}

%init{
    // TODO: code that goes to constructor
%init}

%{
    private Symbol sym(int type)
    {
        return sym(type, yytext());
    }

    private Symbol sym(int type, Object value)
    {
        return new Symbol(type, yyline, yycolumn, value);
    }

    private void error()
    throws IOException
    {
        throw new IOException("Illegal text at line = "+yyline+", column = "+yycolumn+", text = '"+yytext()+"'");
    }
%}

ANY = .

%%

{ANY}       { return sym(sym.ANY); }
"\n" { }

这是我最基本的解析器:

import java_cup.runtime.*;

parser code
{:

    public void syntax_error(Symbol cur_token) {
        System.err.println("syntax_error " + cur_token );
    }

:}

action code
{:
:}

terminal        ANY;

non terminal    grammar;

grammar         ::= ANY : a
                {:
                //System.out.println(a);
                :}
                ;

我正在尝试解析示例文件。我做了一个这样的方法:

AnalyzerLex scanner = null;
        ParserCup pc = null;
        try {
          scanner = new AnalyzerLex( new java.io.FileReader(argv[i]) );
          pc = new ParserCup(scanner);
          while ( !scanner.zzAtEOF ){
              pc.parse_debug();
          }
        }

但是上面的代码会抛出一个错误:

    #2
Unexpected exception:
# Initializing parser
# Current Symbol is #2
# Shift under term #2 to state #2
# Current token is #2
syntax_error #2
# Attempting error recovery
# Finding recovery state on stack
# Pop stack by one, state was # 2
# Pop stack by one, state was # 0
# No recovery state found on stack
# Error recovery fails
Couldn't repair and continue parse at character 0 of input
java.lang.Exception: Can't recover from previous error(s)
    at java_cup.runtime.lr_parser.report_fatal_error(lr_parser.java:375)
    at java_cup.runtime.lr_parser.unrecovered_syntax_error(lr_parser.java:424)
    at java_cup.runtime.lr_parser.debug_parse(lr_parser.java:816)
    at AnalyzerLex.main(AnalyzerLex.java:622)

我认为我没有正确设置词法分析器/解析器。

最佳答案

我不是专家,但我可以建议您采取以下措施:

  1. 您可能必须指定以哪个非终端开始,例如:

    start with compilation_unit;
    
  2. 您可以通过添加行和列来增强您的语法错误方法,这样可以更清楚地了解错误所在。

    public void syntax_error(Symbol s){
        System.out.println("compiler has detected a syntax error at line " + s.left 
            + " column " + s.right);
    }
    

关于parsing - 正确设置 Cup/JLex 解析,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17008139/

相关文章:

java - 从解析器创建抽象树问题

jflex - 解析 CUP 中的 EOF token

jflex - 这个最简单的 jflex 代码有什么问题?

java - 如何为嵌套的 "if"指令定义使用多个破折号的语法?

java - 在Java中解析JSON url

javascript - 在javascript中解析文本文件?或者更好的东西?

android - 在将网页加载到 WebView 之前尝试解析网页的 JSON

c# - 解析字符串 C#(可能使用正则表达式)

java - 当用户定义变量或函数的类型时,Java CUP(解析器)会产生移位/减少冲突

java - 移动/减少 java 杯中的冲突 - 其他问题悬而未决