简体   繁体   中英

LALR(1) and SLR(1) parser

I get a hypothesis from our teacher and he want from us to search and validate it. We have SLR(1) and LALR(1) parser. The hypothesis is:

Suppose we have a language structure called X. If We couldn't provide a LALR(1) grammar for this structure, we couldn't provide a SLR(1) too and maybe a LR(1) grammar could solve problem. but If we could provide a LALR(1) grammar for this structure, we could provide a SLR(1) too.

If you search in internet, you find a lot of sites which say this grammar is not SLR(1) but it is LALR(1):

S -> R
S -> L = R
L -> * R
L -> id
R -> L

("id", "*" and "=" are terminals and others are non-terminals)
If we try to find SLR(1) items, we will see shift/reduce conflict. it is true, but my hypothesis say something else. In our hypothesis, we talk about language described by grammar not grammar itself! We can remove "R" and convert grammar to LL(1) and It is also SLR(1) and LALR(1):

S -> LM
M -> epsilon
M -> = L
L -> * L
L -> id

You can try this grammar and you can see that this grammar describe same language as last grammar and has SLR(1) and LALR(1) grammar!

so my problem is not finding a grammar which is LALR(1) but not SLR(1). There are a lot of them in internet. I want to know is there any language which has LALR(1) grammar but not SLR(1) grammar? and if our hypothesis is true, then there is no need to LALR(1) and SLR(1) could do everything for us, however LALR(1) is easier to use and maybbe in future, a language reject this hypothesis.

I'm sorry for bad English.
Thanks.

Every LR( k ) language has an SLR(1) grammar.

There is a proof in this paper from 1976 , which provides an algorithm for constructing the SLR(1) grammar, if you have an LR( k ) grammar and know the value of k . Unfortunately, there is no algorithm which can tell you definitely whether a CFG is LR( k ), much less provide the value of k . (If you somehow know that the grammar is LR( k ), you can try successive values of k until you find one which works. But that procedure will never terminate if the grammar is not LR( k ).)

The above comes from this reference question on the Computing Science StackExchange site , which is a better place for this kind of question.

LR(1) > LALR(1) > SLR(1)

LR(1) is the most powerful, LALR(1) in less powerful and SLR(1) is least powerful. This is fact, because of the way the lookahead sets are computed. (1) means lookahead of one token. Here is a grammar that is LR(1), but not LALR(1) and definitely not SLR(1):

   G : S... <eof>
     ;
   S : c A1 t ';'
     | c A2 n ';'                       
     | r A2 t ';'
     | r A1 n ';'
     ;
  A1 : a 
     ;
  A2 : a 
     ;

This grammar cannot be made LALR(1) or SLR(1). Or sure you can remove A1 and A2 and replace them with a, but then you have a different grammar. The problem is that an action may be attached to the rule A1 : a and a different action my be attached to A2 : a. For example:

  A1 : a    => X()
     ;
  A2 : a    => Y()
     ;

An SLR(1) parser generator will report conflicts in your grammar that are not real conflicts. I'm talking about the real world using large grammars (eg C11.grm).

The SLR(1) lookahead computation is simplistic, getting the lookaheads from the grammar, instead of the LR(0) state machine created by an LALR(1) parser generator.

That is why Frank DeRemer's paper, in 1969, on LALR(1) is so important.

By looking at the grammar, A1 can be followed by either t or n, so this is a conflict reported by SLR(1), but there is an LR(1) state machine in which there is no conflict on which follows A1.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM