-
8
pages
-
English
-
Documents
Description
A Yacc TutorialVictor EijkhoutJuly 20041 IntroductionThe unix utility yacc (Yet Another Compiler Compiler) parses a stream of token, typicallygenerated by lex, according to a user specified grammar.2 Structure of a yacc fileA yacc file looks much like a lex file:...definitions...%%...rules...%%...code...In the example you just saw, all three sections are present:definitions All code between%{ and%} is copied to the beginning of the resulting C file.rules A number of combinations of pattern and action: if the action is more than a singlecommand it needs to be in braces.code This can be very elaborate, but the main ingredient is the call toyylex, the lexicalanalyser. If the code segment is left out, a default main is used which only callsyylex.3 Definitions sectionThere are three things that can go in the definitions section:C code Any code between %{ and %} is copied to the C file. This is typically used fordefining file variables, and for prototypes of routines that are defined in the codesegment.definitions The definitions section of a lex file was concerned with characters; in yacc thisis tokens. These token definitions are written to a .h file when yacc compiles thisfile.associativity rules These handle associativity and priority of operators.14 Lex Yacc interactionConceptually, lex parses a file of characters and outputs a stream of tokens; yacc accepts astream of tokens and parses it, performing actions as appropriate. In practice, they are moretightly ...
-
Publié par
-
Langue
English