80 likes | 469 Views
Lex Source Program. Lex Compiler. lex.yy.cc. lex.yy.cc. C Compiler. executable file. Input Stream. Exectuable File. Sequence of Tokens. Lex Source Program. Table of regular expressions and corresponding program fragments. Format:. { definitions} %% {rules} %% {user subroutines}.
E N D
Lex Source Program Lex Compiler lex.yy.cc lex.yy.cc C Compiler executable file Input Stream Exectuable File Sequence of Tokens
Lex Source Program Table of regular expressions and corresponding program fragments Format: {definitions} %% {rules} %% {user subroutines}
Definitions section contains a combination of • C code, in the form • %{ • code • %} • used forpreprocessor statements that must begin in column 1, e.g. • %{ • #include <stdio.h> • %} • C code, in the form “blankspace code” e.g. • int students=0; • Definitions, in the form “name blankspace substitute” • e.g. • DIGIT [0-9]+
RULES This section is Lex Action section. When an expression is matched, Lex executes the corresponding action. The form of this section is: pattern action Where: pattern – Lex Regular Expressions action – Corresponding C code
Lex Regular Expressions • Refer to • the documentation written by by M. E. Lesk and E. Schmidt • http://www.site.uottawa.ca/~jyzhao/courses/seg2101/lab5/lex-docs.txt • Part 3(Lex Regular Expressions) and Part 12 (Source Format) • The Unix manual page of FLEX • http://www.site.uottawa.ca/~jyzhao/courses/seg2101/lab5/lex_manpage.pdf • Page 6 (Patterns)
2. You need follow the following steps:2.1. create a lex source file (e.g. test.lex) containing the patterns and actions, by following the required format. Refer to lex_manpage.pdf.2.2. compile test.lex by using flex test.lex2.3. you will find a new file generated by flex, named lex.yy.c2.4. compile by gcc lex.yy.c -lfl -o test.exe2.5. you should get your executable, test.exe2.6. create a text file (e.g. t.txt) containing your inputs2.7. execute test < t.txt2.8. you should be able to get what you expected