What does S-attributed and L-attributed grammar mean?

user397232 picture user397232 · Mar 16, 2010 · Viewed 34.9k times · Source

I'm reading a compiler book and kinda confused when it says "a S-attribute grammar is also a L-attribute grammar". Couldn't understand. Can someone make it clear (an example should be great). Thanks.

Answer

user151323 picture user151323 · Mar 16, 2010

L-attributed grammar

L-attributed grammars are a special type of attribute grammars. They allow the attributes to be evaluated in one left-to-right traversal of the abstract syntax tree. As a result, attribute evaluation in L-attributed grammars can be incorporated conveniently in top-down parsing. Many programming languages are L-attributed. Special types of compilers, the narrow compilers, are based on some form of L-attributed grammar. These are comparable with S-attributed grammars. Used for code synthesis.

S-attributed grammar

S-Attributed Grammars are a class of attribute grammars characterized by having no inherited attributes. Inherited attributes, which must be passed down from parent nodes to children nodes of the abstract syntax tree during the semantic analysis of the parsing process, are a problem for bottom-up parsing because in bottom-up parsing, the parent nodes of the abstract syntax tree are created after creation of all of their children. Attribute evaluation in S-attributed grammars can be incorporated conveniently in both top-down parsing and bottom-up parsing. Yacc is based on the S-attributed approach.

Any S-attributed grammar is also an L-attributed grammar.

In L-attributed grammars attribute evaluation can be performed in left-to-right traversal. Since in S-attributed grammars attributes are not inherited, it does not prevent you from doing just that. As such, you can say an S-attributed grammar conforms to that characteristic of an L-grammar.