是C#和Java语法LALR(X)? [英] Are C# and Java Grammars LALR(x)?

查看:243
本文介绍了是C#和Java语法LALR(X)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道C#和Java语法是LALR(X)?如果是,什么是x的值?

I wonder if C# and Java grammars are LALR(x)? If yes, what's the value of x?

编辑:

在接受真正的答案之后,我觉得这是更好地改变这样的问题:

After accepting the true answer, I think it is better to change the Q in this way:

是否有任何LALR(x)的解析器可以解析的爪哇(7版)或C#(第4版)当前版本?如果是,什么是x的值?

Is there any LALR(x) parser that could parse current releases of Java (version 7) or C# (version 4)? If yes, what is the value of x?

推荐答案

您不能没有先指定一个特定的语法为的langauge问这个问题,因为一些语法可能是,有的可能没有。

You can't ask this question without first designating a specific grammar for a langauge, as some grammars may be, and some may not.

也许你的意思是Java的语法公布在最近的Java规范。你的意思是对Java 7?

Perhaps you mean the Java grammar as published in recent Java specifications. Do you mean for Java 7?

我不知道,你可以指定为C#特定的语法,至少不是一个来自微软,特别是对C#4.0;我不相信他们已经出版了一本语法。

I'm not sure you can designate a specific grammar for C#, at least not one from Microsoft, especially for C# 4.0; I don't believe they have published a grammar.

我可以告诉你,我不认为C#可以LALR(x)的,因为它有一些元素看起来像标识符,但可以在某些情况下的关键字。这就要求词法分析器知道解析器期待来决定是否标识符般的记号是一个关键字,或者只是和标识。因此,必须有从解析器反馈词法分析器或词法分析器具有同时生产标记并传递给解析器来决定哪个就是了。 LALR解析器上记号流定义没有任何反馈,并在每一个输入令牌只有一个跨pretation。

I can tell you that i don't think C# can be LALR(x), because it has some elements which look like identifiers, but can be keywords in certain contexts. This requires the lexer to know what the parser is expecting to decide if an identifier-like token is a keyword, or just and identifier. Thus there has to be feedback from the parser to lexer, or the lexer has to produce both tokens and pass them to the parser to decide which it wants. LALR parsers are defined on token streams without any feedback, and where every input token has only one interpretation.

我不认为Java是,或者说,从Java 1.5及更高版本,当枚举是作为一种特殊类型的拥有自己的关键字。这是因为,对Java 1.5的编译器来处理现有的Java 1.4程序所使用的枚举的作为变量名,枚举的必须被视为在某些情况下一个关键字,和作为在其他变量名。所以,一个Java 1.5解析器相同的问题C#一样。

I don't think Java is, either, from Java 1.5 and up, when enum was introduced as a special type with its own keyword. This is because, for Java 1.5 compilers to process existing Java 1.4 programs that used enum as a variable name, enum must be treated as a keyword in some contexts, and as a variable name in others. So a Java 1.5 parser has the same issues as C# does.

作为一个实际问题,没有真正的汉语语言是LALR(1)[第一版的Java可能是一个例外]和任何人建立一个真正的解析器(ESP LALR)必须做出某种黑客来解决这个问题。 (GCC著名解析C ++与一个可怕的符号表破解了很长时间,所以它可以告诉一个标识符作为变量,一个标识符作为一个typedef实例之间的区别的LALR解析器。它现在有某种手工实现的递归下降解析器,但我觉得可怕的黑客仍然)。于是我回答的价值我不知道你的问题。

As a practical matter, no real langauges are LALR(1) [first edition Java may be an exception] and anybody building a real parser (esp LALR) has to make some kind of hack to get around this. (GCC famously parsed C++ with an LALR parser with an awful symbol table hack for a long time, so it could tell the difference between an identifier as a variable, and an identifier as a typedef instance. It now has some kind of hand-implemented recursive descent parser, but I think the awful hack remains). So I'm not sure the value of answer to your question.

我们的 C#我们的语言面前的家庭的4.0和Java 7个成员端使用GLR分析器两种解析语言,与反馈能力,以及处理同样的两个相互pretations能力扩展两者。 GLR使得LALR(X)没有实际意义的问题,反馈和多间pretations让我们处理许多语言,这将是纯粹的GLR的能力之外了。

Our C# 4.0 and Java 7 members of our family of language front ends both parse the languages using a GLR parser, extended both with the feedback capability, and the ability to process two interpretations of the same token. GLR makes the question of LALR(x) moot, and the feedback and multiple interpretations let us handle many languages that would be outside of pure GLR's capability, too.

编辑:有点想法之后,有可能是一个真正丑陋的方式,使两者的语法处理其关键字的上下文。让我们使用Java的枚举作为一个例子。有现实必须是语法规则:

After a bit of thought, there might be a truly ugly way to make both grammars handle their keyword-in-context. Let's use Java's enum as an example. There realistically has to be grammar rule:

  type = 'enum' '{'  enum_members '}' ;

但是,我们还需要允许枚举作为一个标识符。我们能做到这一点,通过更换终端令牌
标识的一个非终结:

  identifier = IDENTIFIER | 'enum' ;

和坚持标识符由词法分析器产生的终端。现在,至少在词法分析器没有决定如何处理的枚举的;解析器一样。但是,您指定的语法将不得不为此形状,以便甚至有被机会LALR(X)。

and insist that IDENTIFIERs are the terminals produced by the lexer. Now at least the lexer does not have to decide how to treat enum; the parser does. But your designated grammar would have to shaped like this in order to even have a chance of being LALR(x).

我们的解析器,让一些关键字有时被人用作标识符。我们改变了我们的分析引擎如前面所述,以及不这样做了。

Our parsers used to do this to allow some keywords to be used sometimes as identifiers. We changed our parsing engine as described earlier, and don't do this any more.

这篇关于是C#和Java语法LALR(X)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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