Compilation Principle -- Analysis of lex and YACC instances

Source: Internet
Author: User

During this period of time, I have been reflecting on the issue of education, turning out my previous books and reading them well. I have found many things I don't understand and have never really understood.

After reading the lexical analysis and syntax analysis, the more simple it looks, the less difficult it will be. In the past, there was still a lack of practice.

The following is an example of lex and YACC.

The first chapter of lex and YACC (second edition) has an example source code.

Ch1-05.l

% {
/*
* We now build a lexical analyzer to be used by a higher-level parser.
*/

# Include "ch1-05y.h"/* token codes from the parser */

# Define lookup 0/* default-not a defined word type .*/

Int state;

%}

%

/N {state = lookup ;}

//./N {state = lookup;
Return 0;/* end of sentence */
}

^ Verb {state = verb ;}
^ Adj {state = adjective ;}
^ Adv {state = adverb ;}
^ {State = noun ;}
^ Prep {state = preposition ;}
^ Pron {state = pronoun ;}
^ Conj {state = conjunction ;}

[A-Za-Z] + {
If (State! = Lookup ){
Add_word (State, yytext );
} Else {
Switch (lookup_word (yytext )){
Case verb:
Return (verb );
Case adjective:
Return (adjective );
Case adverb:
Return (adverb );
Case noun:
Return (N );
Case preposition:
Return (preposition );
Case pronoun:
Return (pronoun );
Case conjunction:
Return (conjunction );
Default:
Printf ("% s: Don't recognize/N", yytext );
/* Don't return, just ignore it */
}
}
}

.;

%
/* Define a linked list of words and types */
Struct word {
Char * word_name;
Int word_type;
Struct word * next;
};

Struct word * word_list;/* first element in word list */

Extern void * malloc ();

Int
Add_word (INT type, char * word)
{
Struct word * WP;

If (lookup_word (Word )! = Lookup ){
Printf ("!!! Warning: Word % s already defined/N ", word );
Return 0;
}
 
/* Word not there, allocate a new entry and link it on the list */

WP = (struct word *) malloc (sizeof (struct word ));

WP-> next = word_list;

/* Have to copy the word itself as well */
 
WP-> word_name = (char *) malloc (strlen (Word) + 1 );
Strcpy (WP-> word_name, word );
WP-> word_type = type;
Word_list = WP;
Return 1;/* It Worked */
}

Int
Lookup_word (char * word)
{
Struct word * Wp = word_list;

/* Search down the list looking for the word */
For (; WP = WP-> next ){
If (strcmp (WP-> word_name, word) = 0)
Return WP-> word_type;
}

Return lookup;/* not found */
}

Ch1-05.y

% {
/*
* A lexer for the basic grammar to use for recognizing English sentences.
*/
# Include <stdio. h>
%}

% Token noun pronoun verb adverb adjective preposition Conjunction

%
Sentence: Subject verb object {printf ("sentence is valid./N ");}
;

Subject: N
| Pronoun
;

Object: Noun
;
%

Extern file * yyin;

Main ()
{
While (! Feof (yyin )){
Yyparse ();
}
}

Yyerror (s)
Char * s;
{
Fprintf (stderr, "% s/n", S );
}

The ch1-05.y is the YACC program, yyparse () Routine indicates to start the syntax analysis, according to the compilation principle, yyparse will call the yylex of lex,
After yylex is executed and meets the lexical rules, the corresponding action is returned to yyparse.

After compilation and running (In Flex and bison, use cygwin to simulate the environment)

Flex ch1-05.l
MV Lex. XX. c ch1-05.c
Bison-D ch1-05.y
Gcc-g-dyydebug-c-o ch1-05l.o ch1-05l.c
Gcc-g-dyydebug-c-o ch1-05y.o ch1-05y.c
Gcc-g-o ch1-05.pgm ch1-05l.o ch1-05y.o-lfl
Here, lfl is the flex library.

(1) first run./ch1-05.pgm, report segment fault. Use GDB for debugging (! Feof (yyin) occurs
The view of the ch1-05l.c and ch1-05y.c source code plus speculation, it is estimated that the matter yyin has no value, it should be in the yyparse before there is a value.
So the main code of the ch1-05y.c is changed

Main ()
{
Do {
Yyparse ();
} While (! Feof (yyin ))

}

(2) The second run./ch1-05.pgm, enter the following:
Verb is AM
I He pig
He is student
Reporting sentence is valid
Enter again
He is student
Report
Syntax Error

Why?
I thought about it and looked back at the process of syntax analysis lalr and ch1-05.l analysis.
After the call of yyparse is found, after multiple calls of yylex
He is student indicates sentence, but due to the lack of a full stop, a call to yyparse is not completed, and then enter the second he is student, that is
The stack contains sentence, and the input is he is student. The Protocol cannot be modified and syntax errors are reported.

(3) The third run./ch1-05.pgm, enter the following:
Verb is AM
I He pig
He is student
Reporting sentence is valid
Enter again
. If yylex returns 0 after the input, the call to yyparse ends.

He is student.

Report
Sentence is valid.

The above is an example of basic lexical and syntax analysis, which should be somewhat enlightening to everyone.

 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.