(lex) (yacc). (object code) C. (unit).. C,,,,. ( ) (lexical analysis) (lexing). C (routine). (lexical analyzer) (lexer) (scanner). (lex specification).
1. 27 (token descriptions) (regular expressions), grep egrep.. C. 1),., C (expressions), (statements), (declarations), (blocks) (procedures). (parsing), (grammar)., C, (parser). (match), (syntax error).,.., (.. 2 )..,. 1) :. (lex) (lexer)..
28 lex yacc (standard input) : (standard output). \n ECHO; cat (arguments). C,.,,. (code base),.. (, ),.
1. 29. is am are were was be being been do does did will would should can could has have had go [ 1-1]. %{ /* * ( ). * / */ % [\t ]+ /*. */ ; is am are were was be being been do does did will would
30 lex yacc should can could has have had go { printf("%s: is a verb\n", yytext); [a-za-z]+ { printf("%s: is not a verb\n", yytext);. \n { ECHO; /* */ main() { yylex();. (bold). % example1 did I have fun? did: is a verb I: is not a verb have: is a verb fun: is not a verb? ^D %. %{ /* * ( ).
1. 31 * / */ % (definition section) C.. C "%{" "%" (delimiter). C, C. C,. (whitespace)... (rules section)., (pattern) (action).. (regular expressions), grep, sed, ed. 6.. [\t ]+ /*. */ ; "[ ]". ( ) "\t" ( ) " ". "+". ( ). (action), C.
32 lex yacc. " "( ).,. 2). is am are were was be being been do does did should can could has have had go { printf("%s: is a verb\n", yytext);. C printf. yytext. ": is a verb\n".. 2)., foo bar "foo" "bar"..
1. 33 [a-za-z]+ { printf("%s: is not a verb \n", yytext);. \n { ECHO; /* */ "[a-za-z]+",. "-", "-"., ": is not a verb\n". ([a-za-z]).. "island" "is" "island".,.. 1.. 2. (longest possible match). "island" "is", "island" "is".,.. "."( ), "\n". ECHO (punctuation). ECHO,. (,
34 lex yacc ).. (user subroutine section), C. C. main( ). main() { yylex(); yylex( ) C, (main). 3) return, yylex( ). ch1-02.l.. % lex ch1-02.l % cc lex.yy.c -o first -ll lex.yy.c C, C -ll.,.. 3).
1. 35, ' '. [ 1-2]. %{ /* *. */ % [\t ]+ /*. */ ; is am are were was be being been do does did will would should can could has have had go { printf("%s: is a verb\n", yytext); very simply gently quietly calmly
36 lex yacc angrily { printf("%s: is an adverb\n", yytext); to from behind above below between below { printf("%s: is a preposition\n", yytext); if then and but or { printf("%s: is a conjunction\n", yytext); their my your his her its { printf("%s: is an adjective\n", yytext); I you he she we they { printf("%s: is a pronoun\n", yytext); [a-za-z]+ { printf("%s: don't recognize, might be a noun\n", yytext);. \n { ECHO; /* */ main() { yylex();
1. 37.,.,..,,.,.,. noun dog cat horse cow verb chew eat lick (symbol table),., C,,, (enumeration tag).. C,,...,,. (, ) ' (reserved words)'.., add_word( ), lookup_word( ). state, LOOKUP,
38 lex yacc. state, \n state. [ 1-3]. %{ /* * */ enum { LOOKUP = 0, /* -. */ VERB, ADJ, ADV, NOUN, PREP, PRON, CONJ ; int state; int add_word(int type, char *word); int lookup_word(char *word); %, state enum. (enumerated type) state,..
1. 39 [ 1-4]. \n { state = LOOKUP; /*,. */ /* */ /*. */ ^verb { state = VERB; ^adj { state = ADJ; ^adv { state = ADV; ^noun { state = NOUN; ^prep { state = PREP; ^pron { state = PRON; ^conj { state = CONJ; [a-za-z]+ { /*,. */ if(state!= LOOKUP) { /*. */ add_word(state, yytext); else { switch(lookup_word(yytext)) { case VERB: printf("%s: verb\n", yytext); break; case ADJ: printf("%s: adjective\n", yytext); break; case ADV: printf("%s: adverb\n", yytext); break; case NOUN: printf("%s: noun\n", yytext); break; case PREP: printf("%s: preposition\n", yytext); break; case PRON: printf("%s: pronoun\n", yytext); break; case CONJ: printf("%s: conjunction\n", yytext); break; default: printf("%s: don't recognize\n", yytext); break;
40 lex yacc. /*. */ ;, ( "^" ). state LOOKUP. "[a-za-z]+" state LOOKUP lookup_word( ),. state add_word( ). [ 1-5] main( ). main() { yylex(); /*. */ struct word { char *word_name; int word_type; struct word *next; ; struct word *word_list; /* */ extern void *malloc(); int add_word(int type, char *word) { struct word *wp; if(lookup_word(word)!= LOOKUP) {
1. 41 printf("!!! warning: word %s already defined \n", word); return 0; /*,. */ wp = (struct word *) malloc(sizeof(struct word)); wp->next = word_list; /*. */ wp->word_name = (char *) malloc(strlen(word)+1); strcpy(wp->word_name, word); wp->word_type = type; word_list = wp; return 1; /*. */ int lookup_word(char *word) { struct word *wp = word_list; /*. */ for(; wp; wp = wp->next) { if(strcmp(wp->word_name, word) == 0) return wp->word_type; return LOOKUP; /*. */ (linked list).. (hash table)..
42 lex yacc (session). verb is am are was were be being been do is is: verb noun dog cat horse cow verb chew eat lick verb run stand sleep dog run dog: noun run: verb chew eat sleep cow horse chew: verb eat: verb sleep: verb cow: noun horse: noun verb talk talk talk: verb......
1. 43. noun verb noun verb noun. " ". 4),. subject noun pronoun "subject( )" (noun) (pronoun).., (object). object noun,. sentence subject verb object..,. 4) " ",,.,.
44 lex yacc. yylex( ).., yylex( )..,.... (NOUN), (PRONOUN), (VERB), (ADVERB), (ADJECTIVE), (PREPOSITION) (CONJUNCTION). #define.. # define NOUN 257 # define PRONOUN 258 # define VERB 259 # define ADVERB 260 # define ADJECTIVE 261 # define PREPOSITION 262 # define CONJUNCTION 263 0. 0,. C. y.tab.h, MS-DOS ytab.h yytab.h,.
1. 45 [ 1-6]. %{ /* *. */ #include "y.tab.h" /* */ #define LOOKUP 0 /* -. */ int state; % \n { state = LOOKUP; \.\n { state = LOOKUP; return 0; /* */ ^verb { state = VERB; ^adj { state = ADJECTIVE; ^adv { state = ADVERB; ^noun { 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);
46 lex yacc case ADVERB: return(adverb); case NOUN: return(noun); case PREPOSITION: return(preposition); case PRONOUN: return(pronoun); case CONJUNCTION: return(conjunction); default: printf("%s: don't recognize\n", yytext); /*. */ \. ;... add_word() lookup_word()..... return., return. yylex( ). yylex( ),..,.
1. 47. \.\n { state = LOOKUP; return 0; /* */.. main( ). [ 1-7]. %{ /* * */ #include <stdio.h> % %token NOUN PRONOUN VERB ADVERB ADJECTIVE PREPOSITION CONJUNCTION sentence: subject VERB object { printf("sentence is valid.\n"); ; subject: NOUN PRONOUN ; object: NOUN ;
48 lex yacc extern FILE *yyin; main() { do { yyparse(); while(!feof(yyin)); yyerror(s) char *s; { fprintf(stderr, "%s\n", s);. "%{" "%" (literal code block). C (, C C ) ( stdio.h).,..,. C (identifier),... yyparse( ) main( ). yyparse( ), ( 0. ).
1. 49 (production rules) ( ). ":",..,. 0. NOUN object..,. " ",. " " "or( )", (subject) NOUN PRONOUN. "{" "" C.,. sentence. sentence sentence. ( main ). yyparse( ). "subject VERB object". "subject subject"? yyerror( ), error.., yyparse( ).
50 lex yacc. C,. main( ) yyerror( ). yyin. yylex( ). [ 1-8],... %{ #include <stdio.h> % %token NOUN PRONOUN VERB ADVERB ADJECTIVE PREPOSITION CONJUNCTION sentence: simple_sentence { printf("parsed a simple sentence.\n"); compound_sentence { printf("parsed a compound sentence.\n"); ; simple_sentence: subject verb object subject verb object prep_phrase ; compound_sentence: simple_sentence CONJUNCTION simple_sentence compound_sentence CONJUNCTION simple_sentence ; subject: NOUN PRONOUN ADJECTIVE subject ; verb: VERB
1. 51 ADVERB VERB verb VERB ; object: NOUN ADJECTIVE object ; prep_phrase: PREPOSITION NOUN ; extern FILE *yyin; main() { do { yyparse(); while(!feof(yyin)); yyerror(s) char *s; { fprintf(stderr, "%s\n", s); sentence. (simple sentence) (clauses). "and" "but" "if".
52 lex yacc (recursion).,. compound_sentence, verb. compound_sentence.. simple_sentence CONJUNCTION simple_sentence " (clauses)", " ". compound_sentence CONJUNCTION simple_sentence.,,., C. if( a == b ) break; else func(&a);v if, (, a, ==, "a == b" if (expression part), break " " " ".
1. 53. ch1-n.l, N. ch1-m.y, M.,. % lex ch1-n.l % yacc -d ch1-m.y % cc -c lex.yy.c y.tab.c % cc -o example-m.n lex.yy.o y.tab.o -ll, C lex.yy.c. y.tab.c y.tab.h (y.tab.h -d, ). C., /usr/lib/libl.a libl.a -ll. AT&T, ( lex yacc byacc flex -ll ).., (GNU bison), C ch1-m.tab.c ch1-m.tab.h. MS-DOS ( ytab.c ytab.b ). ' A'.
54 lex yacc C.. [ 1-9],,, C. [ 1-10]. C 3., C. #include <stdio.h> #include <ctype.h> char *progname; #define NUMBER 400 #define COMMENT 401 #define TEXT 402 #define COMMAND 403 main(argc,argv) int argc; char *argv[]; { int val; while(val = lexer()) printf("value is %d\n",val); lexer() { int c; while ((c=getchar()) == ' ' c == '\t') ; if (c == EOF)
1. 55 return 0; if (c == '.' isdigit(c)) { /* */ while ((c = getchar())!= EOF && isdigit(c)); if (c == '.') while ((c = getchar())!= EOF && isdigit(c)); ungetc(c, stdin); return NUMBER; if ( c == '#' ) { /* */ int index = 1; while ((c = getchar())!= EOF && c!= '\n'); ungetc(c,stdin); return COMMENT; if ( c == '"' ) { /* */ int index = 1; while ((c = getchar())!= EOF && c!= '"' && c!= '\n'); if(c == '\n') ungetc(c,stdin); return TEXT; if ( isalpha(c)) { /*. */ int index = 1; while ((c = getchar())!= EOF && isalnum(c)); ungetc(c, stdin); return COMMAND; return c; %{ #define NUMBER 400 #define COMMENT 401 #define TEXT 402
56 lex yacc #define COMMAND 403 % [ \t]+ ; [0-9]+ [0-9]+.[0-9]+ \.[0-9]+ { return NUMBER; #* { return COMMENT; \"[^\"\n]*\" { return TEXT; [a-za-z][a-za-z0-9]+ { return COMMAND; \n { return '\n'; return yytext[0]; #include <stdio.h> main(argc,argv) int argc; char *argv[]; { int val; while(val = yylex()) printf("value is %d\n",val);., C. "*", "/". "*" "/",. C "*" "*",. ( ). /** **/
1. 57,,. 2. 3.,. 1.,. 2. "has been". AUXVERB. 3. "watch", "fly", "time" "bear".? NOUN_OR_VERB, subject, verb object.? 4.,.? "ing", "a" "the". 5.??