简体   繁体   English

Rascal:TrafoFields 语法错误:具体语法片段

[英]Rascal: TrafoFields Syntax error: concrete syntax fragment

I'm trying to re-create Tijs' CurryOn16 example "TrafoFields" scraping the code from the video, but using the Java18.rsc grammar instead of his Java15.rsc.我正在尝试重新创建 Tijs 的CurryOn16示例“TrafoFields”,从视频中抓取代码,但使用 Java18.rsc 语法而不是他的 Java15.rsc。 I've parsed the Example.java successfully in the repl, like he did in the video, yielding a var pt .我在 repl 中成功解析了 Example.java ,就像他在视频中所做的那样,产生了一个 var pt I then try to do the transformation with trafoFields(pt) .然后我尝试使用trafoFields(pt) The response I get is:我得到的回应是:

|project://Rascal-Test/src/TrafoFields.rsc|(235,142,<12,9>,<16,11>): Syntax error: concrete syntax fragment

My TrafoFields.rsc looks like this:我的 TrafoFields.rsc 看起来像这样:

module TrafoFields

import lang::java::\syntax::Java18;

/**
 * - Make public fields private
 * - add getters and setters
 */

 start[CompilationUnit] trafoFields(start[CompilationUnit] cu) {
    return innermost visit (cu) {
        case  (ClassBody)`{
                         '  <ClassBodyDeclaration* cs1>
                         '  public <Type t> <ID f>;
                         '  <ClassBodyDeclaration* cs2>
                         '}`
         =>  (ClassBody)`{
                         '  <ClassBodyDeclaration* cs1>
                         '  private <Type t> <ID f>;
                         '  public void <ID setter>(<Type t> x) {
                         '    this.<ID f> = x;
                         '  }
                         '  public <Type t> <ID getter>() {
                         '      return this.<ID f>;
                         '  }
                         '  <ClassBodyDeclaration* cs2>
                         '}`
         when
            ID setter := [ID]"set<f>",
            ID getter := [ID]"get<f>"
    }
 }

The only deviation from Tijs' code is that I've changed ClassBodyDec* to ClassBodyDeclaration* , as the grammar has this as a non-terminal.与 Tijs 代码的唯一偏差是我将ClassBodyDec*更改为ClassBodyDeclaration* ,因为语法将此作为非终端。 Any hint what else could be wrong?任何提示还有什么可能是错误的?

UPDATE更新

More non-terminal re-writing adapting to Java18 grammar:更多适配Java18语法的非终端重写:

  • Id => ID身份证=>身份证

Ah yes, that is the Achilles-heal of concrete syntax usability;啊,是的,这就是具体语法可用性的致命弱点; parse errors.解析错误。

Note that a generalized parser (such as GLL which Rascal uses), simulates "unlimited lookahead" and so a parse error may be reported a few characters or even a few lines after the actual cause (but never before!).请注意,通用解析器(例如 Rascal 使用的 GLL)模拟“无限前瞻”,因此可能会在实际原因之后的几个字符甚至几行(但之前不会!)报告解析错误。 So shortening the example (delta debugging) will help localize the cause.因此缩短示例(增量调试)将有助于定位原因。

My way-of-life in this is:我的生活方式是:

  1. First replace all pattern holes by concrete Java snippets.首先用具体的 Java 片段替换所有模式漏洞。 I know Java, so I should be able to write a correct fragment that would have matched the holes.我知道 Java,所以我应该能够编写一个与漏洞匹配的正确片段。
  2. If there is still a parse error, now you check the top-non-terminal.如果仍然存在解析错误,现在您检查顶部非终端。 Is it the one you needed?是你需要的吗? also make sure there is no extra whitespace before the start and after the end of the fragment inside the backquotes.还要确保在反引号内的片段开始之前和结束之后没有多余的空格。 Still a parse error?还是解析错误? Write a shorter fragment first for a sub-nonterminal first.首先为子非终结符先写一个较短的片段。
  3. Parse error solved?解析错误解决了吗? this means one of the pattern holes was not syntactically correct.这意味着其中一个模式漏洞在语法上不正确。 The type of the hole is leading here, it should be one of the non-terminals used the grammar literally, and of course at the right spot in the fragment.孔的类型在这里领先,它应该是字面上使用语法的非终结符之一,当然在片段中的正确位置。 Add the holes back in one-by-one until you hit the error again.一个一个地添加孔,直到再次遇到错误。 Then you know the cause and probably also the fix.然后你知道原因,也可能知道修复方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 给定 Rascal 中的具体语法,如何生成语言的任意实例? - How to generate arbitrary instances of a language given its concrete syntax in Rascal? if语句中的rascal语法错误(newbie q) - Rascal syntax error in if statement (newbie q) 如何在Rascal语法规范中表达SDF2置换短语语法(&lt;&lt; .. &gt;&gt;) - How to express SDF2 permutation phrase syntax (<<..>>) in Rascal syntax specifications Rascal:语法定义文档中的“标签”和“名称”有什么区别? - Rascal: What is the difference between "label" and "name" in the Syntax Definition documentation? 在Rascal中,有没有一种方法可以从其对应的数据类型生成语法的语法定义? - Is there a way in Rascal to generate the syntax definition of a grammar from its corresponding datatype? 在具体的语法模式匹配中可以进行哪些操作? - What operations are possible in concrete syntax pattern matching? 如何将具体的语法值转换为其他类型的值? - How I can convert concrete syntax values to other kinds of values? 如何构造X的具体语法值? - How can I contruct a `X?` concrete syntax value? 缺少安装工件的Rascal错误 - Rascal error installing artifact missing Rascal / Eclipse集成错误(无法在Eclipse Kepler中加载Rascal透视图) - Rascal/Eclipse integration error (Unable to load Rascal perspective in Eclipse Kepler)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM