[antlr-interest] lazy parser code writing

Jigang (Robert) Sun sunjigang1965 at yahoo.com.cn
Thu Oct 26 10:42:18 PDT 2006


Hi,
I am writing parser by hand. I want to be lazy, i.e. I write less code. For example, for the
following grammar

SpecialExpression = BasicValue
| List

BasicValue = INT
|BOOL
|REAL

List = ListDenotation
| DotDotexpression

ListDenotation = "["[ListKind] [{LGraphExpr}-list [: GraphExpr]] [SpineStrictness] "]"
LGraphExpr = GraphExpr| CharsDenotation

DotDotexpression = "["[ListKind] GraphExpr [,GraphExpr]..[GraphExpr] [SpineStrictness] "]"

the best parsing method is, at each level, to decide exactly the next route to go, for 
SpecialExpression = BasicValue
| List

the usual method is: when the parser is at a point to parse SpecialExpression, the parser should
check the first token type met,if the token is a token that starts BasicValue(is of INT, REAL or
BOOL) then parse BasicValue, if the first token is "[" then parse List. 

The method I want to adopt is not do token type checking, instead go straight to parse BasicValue,
let BasicValue parsing method check if the first token is of INT, REAL or BOOL, if not then return
null; then parse List. The token type checking is done at lowerest level, and only once.

The advantage is that the program code is short, because I ommit the token type checking that is
repeatedly done at a further lower levels, e.g. at List parsing and further down List, i.e.
ListDenotation and DotDotexpression.

Maybe the disadvantage is slower speed.

How about the method?

Thanks.
Jigang


 



		
___________________________________________________________ 
抢注雅虎免费邮箱-3.5G容量,20M附件! 
http://cn.mail.yahoo.com


More information about the antlr-interest mailing list