JavaCC LOOKAHEAD option

本文深入解析LOOKAHEAD在解决解析冲突中的应用,包括全局与局部LOOKAHEAD的区别,何时及如何使用LOOKAHEAD,并通过具体示例进行说明。

What's LOOKAHEAD ?


It's used to resolve conflict. For example:

PARSER_BEGIN(Parser)import java.io.*;import java.util.*;public class Parser{    public static void main(String[] args) throws ParseException, FileNotFoundException     {        Parser parser = new Parser(new FileInputStream(args[0]));        parser.Input();    }    public static boolean yes() {        return true;    }    public static boolean no() {        return false;    }}PARSER_END(Parser)TOKEN:{    <A: "a">  | <B: "b">}void Input() :{}{  LOOKAHEAD(…) AB() | A() B()}void A() :{}{  <A>  }void B() :{}{  <B>  }void AB() :{}{  <A> <B>  }



The input "a b" can match both

Choice 1: AB() and

Choice 2: (A() B())

So we must use LOOKAHEAD to distinguish them.


Two types of LOOKAHEAD

Global LOOKAHEAD


Set a global LOOKAHEAD specification by using the option "LOOKAHEAD" either from the command line or at the beginning of the grammar files in the options section. The value of this option is an integer which is the number of tokens to look ahead when making choice decisions.



Local LOOKAHEAD

Set a local LOOKAHEAD specification that affects only a specific choice point.



When a LOOKAHEAD is required?



There are 4 different kinds of choicepoints in JavaCC where a LOOKAHEAD can be used:

1.      ( exp1 | exp2 | ... )

2.      [ exp ], or ( exp )?

3.      ( exp )*

4.      ( exp )+



The LOOKAHEAD is not needed when there is no choice, such as

void P() :

{}

{

   LOOKAHEAD(...) <HELLO> <WORLD>

}


LOOKAHEAD syntax



The general structure of a LOOKAHEADspecification is:

LOOKAHEAD( amount, expansion, { boolean_expression } )


l  amount: an integer specifies the number of tokens to LOOKAHEAD

l  expansion: specifies the expansion to use to perform syntacticLOOKAHEAD

l  boolean_expression: is the Boolean expression to use for semanticLOOKAHEAD; for example it can be a simple Boolean java function.


It means look ahead amount token, if it prefix match the expansion, and boolean_expressionreturn true, then use the choice.


Among the 3 entries, at least one of themmust be present; and others will have default value:

"mount":

2147483647        - if"expansion" is present

0                 - if "boolean_expression" ispresent

Note: When"amount" is 0, no syntactic LOOKAHEAD is performed.  Also, "amount" does not affect thesemantic LOOKAHEAD.

"expansion":          - defaults to theexpansion being considered.

"boolean_expression": - defaultsto true.



Samples for above grammar case


1.      LOOKAHEAD(1, ( <A><C>), {yes()})

Choice 1:lookahead first 1 token, that prefix match (<A> <C>), and yes()return true.

2.      LOOKAHEAD(2, ( <A><C>), {yes()})

Choice 2:lookahead first 2 token, that not prefix match (<A> <C>), and yes()return true.

3.      LOOKAHEAD(3, ( <A> <B>),{yes()})

Choice 1:lookahead first 1 token, that prefix match (<A> <C>), and yes()return true.

4.      LOOKAHEAD(1, ( <A> ),{yes()})

Choice 1:lookahead first 1 token, that prefix match (<A> <C>), and yes()return true.

5.      LOOKAHEAD(2, ( <A> ),{yes()})

Choice 1:lookahead first 1 token, that prefix match (<A> <C>), and yes()return true.



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值