由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Java版 - 清教关于编译原理
相关主题
Re: java enclosure是什么-今天被hm问倒了Re: Is java using call by reference?
invoke a function dynamically in Java?问一个JavaScript的菜问题
请教,Java里有没有range函数功能?请帮忙看看这个编译错误
再请教一个lucene的问题再问generic问题:tomcat编译错误
JavaCC/SableCC/otherCC ?新手问一个弱问题, 关于从stdin输入int或者其他数值的实现方法
javaccregular expression for whitespace in path
Re: Swing ButtonConcurrentModificationException
谁知道JAVA语音识别怎么做?How to prevent double submission in web form?
相关话题的讨论汇总
话题: func话题: function话题: 函数话题: variable话题: 定义
进入Java版参与讨论
1 (共1页)
f****y
发帖数: 70
1
我用javacc做一个简单语法分析
token定义如下
TOKEN :
{

|
|
|
< FUNC: ["a"-"z"] ( ["a"-"z","A"-"Z","_","0"-"9"] )* >
|

|
< ID: ( ["a"-"z","A"-"Z","_","0"-"9"] )+ >
}
FUNC指函数名,ID指任意字符窜,就是说这样的情况是合法的FUNC(ID)

不过这样的问题是ID的定义包含FUNC,就是在解析的时候,比如遇到test,就不知道是
FUNC还是ID。
这里规定如果函数如果没有参数就省去括号,比如done和test(d)都可以是函数。
所以不知道如何定义,函数名和ID可以解析的时候可以区别。
c****r
发帖数: 185
2
In general, ID refers to both function ID and variable ID.
To differetiate them, one way is that function ID is always followed
by "(" like java or c.
Another way is whenever you declare a function or variable, add the ID
to the symbol table. When you encounter the ID, you can look up the symbol
table and see if it is function ID or variable ID.

【在 f****y 的大作中提到】
: 我用javacc做一个简单语法分析
: token定义如下
: TOKEN :
: {
:
: |
: |
: |
: < FUNC: ["a"-"z"] ( ["a"-"z","A"-"Z","_","0"-"9"] )* >
: |

f****y
发帖数: 70
3
hi, thanks for suggestion.
1)这里的函数和java,c的有点不一样,就是不带参数的时候,省去括号,这样形式上就
和变量一样了。
如果我定义函数这样,function-> FuncID|(func"("),就是带括号,或没有括号,这样
还是存在FuncID和ID冲突的问题。
这样如何解决。
这里函数名一定是小写字母开始,ID是任意数字,字母组合。
2)第二个方法,我不是很明白。怎么能区别function ID 还是 variable ID?

【在 c****r 的大作中提到】
: In general, ID refers to both function ID and variable ID.
: To differetiate them, one way is that function ID is always followed
: by "(" like java or c.
: Another way is whenever you declare a function or variable, add the ID
: to the symbol table. When you encounter the ID, you can look up the symbol
: table and see if it is function ID or variable ID.

c****r
发帖数: 185
4
If the grammar is ambiguous then it is impossible to differentiate them.
For example,
f(x)=x+1; //declaring a function
f=10; //is it a function or variable?
So you have to make your grammar clear, like
function f = 10; //"function" is a reserved word
or
f { // functions are followed by "{"
return 10;
}
In the second case, you can lookahead "{"



道是


【在 f****y 的大作中提到】
: hi, thanks for suggestion.
: 1)这里的函数和java,c的有点不一样,就是不带参数的时候,省去括号,这样形式上就
: 和变量一样了。
: 如果我定义函数这样,function-> FuncID|(func"("),就是带括号,或没有括号,这样
: 还是存在FuncID和ID冲突的问题。
: 这样如何解决。
: 这里函数名一定是小写字母开始,ID是任意数字,字母组合。
: 2)第二个方法,我不是很明白。怎么能区别function ID 还是 variable ID?

f****y
发帖数: 70
5
yes. I understand what you are saying. Thanks I will look it further.

【在 c****r 的大作中提到】
: If the grammar is ambiguous then it is impossible to differentiate them.
: For example,
: f(x)=x+1; //declaring a function
: f=10; //is it a function or variable?
: So you have to make your grammar clear, like
: function f = 10; //"function" is a reserved word
: or
: f { // functions are followed by "{"
: return 10;
: }

f****y
发帖数: 70
6
语法定义里有关键字,比如if,then之类,比如java不会允许变量名是这样底关键字,这种
情况是怎么实现的,怎么处理的?
另外一个问题,正则表示里有没有与的表示,比如或是|. 要定义一个变量是任意字母组合
,但是不能是"good"(比如),这种怎么表示的?

【在 c****r 的大作中提到】
: If the grammar is ambiguous then it is impossible to differentiate them.
: For example,
: f(x)=x+1; //declaring a function
: f=10; //is it a function or variable?
: So you have to make your grammar clear, like
: function f = 10; //"function" is a reserved word
: or
: f { // functions are followed by "{"
: return 10;
: }

d******p
发帖数: 24
7
你做词法分析时,先看是不是关键词,再识别标示符。
就是说,识别关键词的规则的优先级要比识别标示符
的规则优先级高。

【在 f****y 的大作中提到】
: 语法定义里有关键字,比如if,then之类,比如java不会允许变量名是这样底关键字,这种
: 情况是怎么实现的,怎么处理的?
: 另外一个问题,正则表示里有没有与的表示,比如或是|. 要定义一个变量是任意字母组合
: ,但是不能是"good"(比如),这种怎么表示的?

1 (共1页)
进入Java版参与讨论
相关主题
How to prevent double submission in web form?JavaCC/SableCC/otherCC ?
A question about how to segment intput text filejavacc
什么是functionality test?Re: Swing Button
httpsession 问题谁知道JAVA语音识别怎么做?
Re: java enclosure是什么-今天被hm问倒了Re: Is java using call by reference?
invoke a function dynamically in Java?问一个JavaScript的菜问题
请教,Java里有没有range函数功能?请帮忙看看这个编译错误
再请教一个lucene的问题再问generic问题:tomcat编译错误
相关话题的讨论汇总
话题: func话题: function话题: 函数话题: variable话题: 定义