the tokens arevoid main() { printf("Hello World\n"); }
void, main, (, ), {, printf, (, "Hello World\n", ), ;, }
The grammatical categories are: S, NP, VP, D, N, V.
The words are: a, the, cat, mouse, ball, boy, girl, ran, bounced, caught.
The grammar rules are:S --> NP VP NP --> N NP --> D N VP --> V VP --> V NP V --> ran | bounced | caught D --> a | the N --> cat | mouse | ball | boy | girl
- Using the grammar above the sentence the cat caught the mouse can be generated as follows:
S ==> NP VP ==> D N VP ==> the N VP ==> the cat VP ==> the cat V NP ==> the cat caught NP ==> the cat caught D N ==> the cat caught the N ==> the cat caught the mouse- This derivation is performed in a leftmost manner. That is, in each step the leftmost variable in the sentential form is replaced.
- Sometimes a derivation is more readable if it is displayed in the form of a derivation tree.
S / \ NP VP /\ /\ D N V NP / / / \ the cat caught \ /\ D N \ \ the mouse
V = { E } T = { c, id, +, *, (, ) } P = {E --> c, E --> id, E --> (E), E --> E + E, E --> E * E } S = E
E /|\ E + E / /|\ id ( E ) /|\ E * E / \ id id
PARSE TREE UNRECOGNIZED INPUT S the cat caught the mouse /\ NP VP the cat caught the mouse / \ \ D N \ the cat caught the mouse | | \ the | \ cat caught the mouse | \ cat \ caught the mouse /\ V NP caught the mouse | \ caught \ the mouse /\ D N the mouse | | the | mouse | mouse
PARSE TREE UNRECOGNIZED INPUT the cat caught the mouse the cat caught the mouse | D cat caught the mouse | | cat caught the mouse | | | N caught the mouse \ / NP caught the mouse | | caught the mouse | | | V the mouse | | | | the mouse | | | | | D mouse | | | | | | mouse | | | | | | | N | | \ / | | NP | \ / | VP \ / S
STACK INPUT RULE/ACTION E] id+id*id] pop & push using E --> E+E E+E] id+id*id] pop & push using E --> id id+E] id+id*id] pop & consume +E] +id*id] pop & consume E] id*id] pop & push using E --> E*E E*E] id*id] pop & push using E --> id id*E] id*id] pop & consume *E] *id] pop & consume E] id] pop & push using E --> id id] id] pop & consume ] ] accept
egrep foo file
returns all the lines that contain a string matching the expression "foo" in the file "file".
STDIN
instead of having it search a file. For example,
ls | egrep blah
lists all files in the current
directory containing the string "blah"
>cat file
big
bad bug
bag
bigger
boogy
>egrep 'b.g' file
big
bad bug
bag
bigger
the expression consisting of a character followed by a star matches any number (possibly zero) of repetitions of that character. In particular, .* matches any string, and hence acts as a "wildcard".
The File for These Examples
|
Wildcards #1
|
Wildcards #2
|
Wildcards #3
|
Frederic Smith
or Fred Smith
. In other
words, the letters eric
are "optional".
An escaped character is a character preceded by a backslash. The preceding backslash removes an implied special meaning from a character
To search for a line containing text hello.gif
, the
correct command is
egrep 'hello\.gif' file
since egrep 'hello.gif' file
will match lines containing
hello-gif , hello1gif , helloagif
, etc.
Fred
or Frederic
an expression consisting of a character followed by a question mark matches one or zero instances of that character.
bugg?y
matches all of the following:
bugy, buggy
but not bugggy
An expression surrounded by parentheses is treated by a single character.
'Fred(eric)? Smith'
matches Fred Smith
or Frederic Smith
egrep 'Fred(eric)? Smith' file
'[Hh]ello'
matches lines containinghello
orHello
[0-3]
is the same as[0123]
[a-k]
is the same as[abcdefghijk]
[A-C]
is the same as[ABC]
[A-Ca-k]
is the same as[ABCabcdefghijk]
egrep '[^aeiou]' file
returns any line containing a
consonant---not very useful.
>cat file hello hello world hhello >egrep 'hello' file hello hello world hhello
The $ character matches the end of the line. The ^ character matches the beginning of the line.
egrep '^[ ]*hello[ ]*$' file
egrep '^[^aeiou]*$' file
Returns all lines that contain
no vowels.
The expression consisting of two expressions separated by the or operator |
matches lines containing either of those two expressions.
egrep 'cat|dog' file
matches lines containing the word "cat" or the word "dog"
egrep 'I am a (cat|dog)'
matches lines containing the string
"I am a cat" or the string "I am a dog".
? \ . [ ] ^ $ * ( )
[]12]
matches ] , 1, or 2. egrep "!" file
will often produce an error (since the shell thinks that "!" is referring
to the shell command history) while egrep '!' file
will not.
egrep "$HOME" file
searches file for the name of your home directory, while
egrep '$HOME' file
searches for the string $HOME
For another source of information on using egrep see An introduction to UNIX by Dean Brock