Lex & Yacc

Download as pdf or txt
Download as pdf or txt
You are on page 1of 1

Lex & Yacc

06/09/2011 11:16

Lex Practice
Metacharacter Matches

. \n * + ? ^ $ a|b (ab)+ "a+b" []

any character except newline newline zero or more copies of the preceding expression one or more copies of the preceding expression zero or one copy of the preceding expression beginning of line end of line

a or b
one or more copies of ab (grouping) literal "a+b" (C escapes still work) character class Table 1: Pattern Matching Primitives

Expression

Matches

abc abc* abc+ a(bc)+ a(bc)? [abc] [a-z] [a\-z] [-az] [ \t\n]+ [^ab] [a^b] [a|b] a|b

abc ab abc abcc abccc ... abc, abcc, abccc, abcccc, ... abc, abcbc, abcbcbc, ... a, abc
one of: a, b, c any letter, a through z one of: a, -, z one of: - a z

[A-Za-z0-9]+ one or more alphanumeric characters


whitespace anything except: a, b

a, ^, b a, |, b a, b
Table 2: Pattern Matching Examples

Regular expressions in lex are composed of metacharacters (Table 1). Pattern-matching examples are shown in Table 2. Within a character class normal operators lose their meaning. Two operators allowed in a character class are the hyphen ("-") and circumflex ("^"). When used between two characters the hyphen represents a range of characters. The circumflex, when used as the first character, negates the expression. If two patterns match the same string the longest match wins. In case both matches are the same length, then the first pattern listed is used.
http://epaperpress.com/lexandyacc/index.html Page 1 of 4

You might also like