compiler-1(Lexical and yacc)

compiler-1(Lexical and yacc) - Lex :A Lexical Analyzer...

Info iconThis preview shows pages 1–9. Sign up to view the full content.

View Full Document Right Arrow Icon
Lex :A Lexical Analyzer Generator we will use the Lex Analyzer generator to generate a C program from. The most popular version of Lex is called flex {for Fast Lex). It is distributed as part of the Gnu compiler package produced by the Free Software Foundation, and is also freely available at many Internet sites.
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Lex :A Lexical Analyzer Generator Lex is a program: I nput : a text file containing regular expressions, together with the actions to be taken when each expression is matched Output : Contains C source code defining a procedure yylex that is a table-driven implementation of a DFA corresponding to the regular expressions of the input file .
Background image of page 2
Lex :A Lexical Analyzer Generator The format of a Lex input file { definitions } %% { rules } %% { auxiliary routines}
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Lex :A Lexical Analyzer Generator 1 The first section :definitions The definition section occurs before the first % %. 1) any C code that must be inserted external to any function should appear in this section between the delimiters %{and %}, ( Note the order of these characters!) 2) names for regular expressions must also be defined in this section.
Background image of page 4
Lex :A Lexical Analyzer Generator 2 The second section : rules These consist of a sequence of regular expressions followed by the C code that is to be executed when the corresponding regular expression is matched. 3 The third section: auxiliary routines Routines are called in the second section and not defined elsewhere.
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Lex :A Lexical Analyzer Generator 1 Lex allows the matching of single characters, or strings of characters, simply by writing the characters in sequence. Quotes can also be written around characters that are not metacharacters, where they have no effect. For example : if and “if” are same meaning. to match the character sequence (* we would have to write \(\* or " (* ". a special meaning: \n matches a newline and \t matches a tab.
Background image of page 6
Lex :A Lexical Analyzer Generator 2 Complementary sets—that is, sets that do not contain certain characters using the carat ^ as the first character inside the brackets. Thus, [^0-9abc] means any character that is not a digit and is not one of the letters a, b, or c .
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Lex :A Lexical Analyzer Generator 3 One curious feature in Lex is that inside square brackets (representing a character class), most of the metacharacters lose their special status and do not need to be quoted. (1) we could have written [ -+ ] instead of ( "+" | "-" ) . (but not [+-] because of the metacharacter use of - to express a range of characters). (2) [."?] means any of the three characters period, quotation mark, or question mark (all three of these characters have lost their metacharacter meaning inside the brackets). (3)
Background image of page 8
Image of page 9
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 10/20/2011 for the course COMPUTER S 10586 taught by Professor Jilinwang during the Spring '09 term at Zhejiang University.

Page1 / 34

compiler-1(Lexical and yacc) - Lex :A Lexical Analyzer...

This preview shows document pages 1 - 9. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online