0% found this document useful (0 votes)
17 views7 pages

‏لقطة شاشة ٢٠٢٤-٠٣-٢١ في ٩.١٢.٣٢ ص

The document provides instructions for setting up Lex and Yacc tools on Ubuntu via terminal commands, including installation steps for both tools and alternatives if errors occur. It also explains how to compile and run a Lex file, detailing the structure of a lex specification and the use of regular expressions for lexical analysis. Additionally, it covers special functions and rules for token matching in Lex.

Uploaded by

We79had
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views7 pages

‏لقطة شاشة ٢٠٢٤-٠٣-٢١ في ٩.١٢.٣٢ ص

The document provides instructions for setting up Lex and Yacc tools on Ubuntu via terminal commands, including installation steps for both tools and alternatives if errors occur. It also explains how to compile and run a Lex file, detailing the structure of a lex specification and the use of regular expressions for lexical analysis. Additionally, it covers special functions and rules for token matching in Lex.

Uploaded by

We79had
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

3.

Setting up Lex and Yacc Tools via Terminal


Commands.
■ Connect your Ubuntu to the Internet then open terminal (Ctrl+Alt+t)
■ Type the following command: sudoapt-get update

■ Type the password for your Linux Account and press Enter, if ask for your password.
■ Type “y” and press Enter, ifask for your permissions to update.

20
3. Setting up Lex and Ycc Tools via Terminal
Commands.

• Toinstall Lex tool, type the following command in terminal :


sudo apt-get install flex
• Type “y” and press Enter, if ask for confirmation.
• To install Yacc tool, type the following command in terminal :
sudo apt-get install bison
• If the servers of bison are down and give errors, you can use any
of the following alternatives commands:
• sudo apt-get install byacc
• sudo apt-get install bison++
• sudo apt-get install byacc-j
21
Compiling and Running Lex File

■ Right-click on the desktop and open a new document.

■ Rename the file with ( test.l ), the file extension is small L.

■ Write the code shown in the next slide.

22
LEX
1. Lex: a tool for automatically generating a lexer or scanner given a lex
specification (.l file)

2. A lexer or scanner is used to perform lexical analysis, or the breaking up


of an input stream into meaningful units, or tokens.

3. For example, consider breaking a text file up into individual words.

Skeleton of a lex specification (.l file)


%{

< C global variables, prototypes, comments >

%}

[DEFINITION SECTION]

%%

[RULES SECTION]

%%

< C auxiliary subroutines>

The rules section


%%

[RULES SECTION]

<pattern> { <action to take when matched> }

<pattern> { <action to take when matched> }

%%

Prepared by: Ms. Rincy Merlin Mathew Page 3


Patterns are specified by regular expressions.

For example:

%%

[A-Za-z]* { printf(“this is a word”); }

%%

Regular Expression Basics


. : matches any single character except \n

* : matches 0 or more instances of the preceding regular expression

+ : matches 1 or more instances of the preceding regular expression

? : matches 0 or 1 of the preceding regular expression

| : matches the preceding or following regular expression

[ ] : defines a character class

() : groups enclosed regular expression into a new regular expression

“…”: matches everything within the “ “ literally

x|y x or y

{i} definition of i

x/y x, only if followed by y (y not removed from input)

x{m,n} m to n occurrences of x

x x, but only at beginning of line

x$ x, but only at end of line

"s" exactly what is in the quotes (except for "\" and following character)

A regular expression finishes with a space, tab or newline

Prepared by: Ms. Rincy Merlin Mathew Page 4


Meta-characters

– meta-characters (do not match themselves, because they are used in the
preceding reg exps):

» ( )[]{}<>+/,^*|. \"$?-%

– to match a meta-character, prefix with "\"

– to match a backslash, tab or newline, use \\, \t, or \n

Regular Expression Examples

• an integer: 12345

[1-9][0-9]*

• a word: cat

[a-zA-Z]+

• a (possibly) signed integer: 12345 or -12345

[-+]?[1-9][0-9]*

• a floating point number: 1.2345

[0-9]*”.”[0-9]+

Lex Regular Expressions


Lex uses an extended form of regular expression:

(c: character, x,y: regular expressions, s: string, m,n integers and i: identifier).

1. c any character except meta-characters (see below)

2. [...] the list of enclosed chars (may be a range)

3. [...] the list of chars not enclosed

4. . any ASCII char except newline

5. xy concatenation of x and y

6. x* same as x*

7. x+ same as x+ (i.e. x* but not )

8. x? an optional x (same as x+ )

Prepared by: Ms. Rincy Merlin Mathew Page 5


Two Rules

1. lex will always match the longest (number of characters) token possible.

2. If two or more possible tokens are of the same length, then the token with the
regular expression that is defined first in the lex specification is favored.

Regular Expression Examples

• a delimiter for an English sentence


“.” | “?” | ! OR [“.””?”!]

• C++ comment: // call foo() here!!


“//”.*

• white space
[ \t]+

• English sentence: Look at this!


([ \t]+|[a-zA-Z]+)+(“.”|”?”|!)

Special Functions
• yytext
– where text matched most recently is stored
• yyleng
– number of characters in text most recently matched
• yylval
– associated value of current token
• yymore()
– append next string matched to current contents of yytext
• yyless(n)
– remove from yytext all but the first n characters
• unput(c)
– return character c to input stream
• yywrap()
– may be replaced by user
– The yywrap method is called by the lexical analyser whenever it inputs
an EOF as the first character when trying to match a regular expression

Prepared by: Ms. Rincy Merlin Mathew Page 6

You might also like