1 / 17

Compiler/fortolker struktur

Compiler/fortolker struktur. If token Ident token. if viggo == 3 then. Lexikalsk- analyse. Syntax- analyse. Semantik- analyse. - - - - - - - Front end - - - - - - - - -. Mellem- kode. Kode- generering. Kode- optimering. Leksikalsk analyse. Strukturanalyse af sproget.

karl
Download Presentation

Compiler/fortolker struktur

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Compiler/fortolker struktur If token Ident token if viggo == 3 then .... Lexikalsk- analyse Syntax- analyse Semantik- analyse - - - - - - - Front end - - - - - - - - - Mellem- kode Kode- generering Kode- optimering

  2. Leksikalsk analyse. Strukturanalyse af sproget Leksikalsk- analyse Syntax- analyse Strukturanalyse er baseret på grammatik, hvor de enkelte blade i parsertræet er de enkelte tegn i alfabetet. Context-free grammatik statement -> ident ’=’ exp ’;’ . . . ident -> letter tegnlist tegnlist -> e | tegnlist tegn tegn -> letter | digit letter -> ’a’|’b’|’c’|......|’A’|’B’|’C’|...|’Å’ digit -> ’0’|’1’|’2’|’3’|’4’|’5’.... Regulær grammatik

  3. Leksikalsk analyse Opgave: Opdel sproget i leksikalske enheder (ord) der kan beskriver vha. regulær grammatik. Fx: if then else void -1.34e-5 Variabel_1 . . Gemme stavemåde for IdT og værdi for fx RealT.

  4. Ad hoc lekser. int yylval; char ch; struct symnote *lookup(char *s); struct symnote *insert_sym(char *s); char yytext[80]; void lex_init() {ch = getchar();} main() { struct symnote *sptr; init_sym(); sptr = insert_sym(”IF"); sptr->type = ifT; sptr = insert_sym(”THEN"); sptr->type = thenT; sptr = insert_sym(”ELSE"); sptr->type = elseT; lex_init(); yylex(); }

  5. Symboltabel struct symnote { char *name; int type; double value; void *address; char used; char assigned; ...... }; struct symnote *symboltabel[TABSIZE];

  6. Ad hoc lekser. int yylex() { struct symnote *sptr; int i; switch (ch) { case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': { yylval = ch - '0'; while (isdigit(ch = getchar())) yylval = yylval * 10 + ch - '0'; return intT; }

  7. Ad hoc lekser. case 'A': case 'B': case 'C': case 'D': . . case 'Z': { i = 0; yytext[i] = ch; while (isupper(ch = getchar())) { yytext[++i] = ch; } yytext[++i] = '\0'; sptr = lookup(yytext); if (sptr != NULL) { return sptr->type; } return IdT; }

  8. Ad hoc lekser. case ' ': case '\n': case '\t':ch = getchar(); return yylex(); case '+': ch = getchar(); return addT; case '=': ch = getchar(); return eqT; default: ch = getchar(); return unknownT; } }

  9. Endelige automater. Den rigtige metode FSM. [a-z]([a-z]|[0-9]|_)* [a-z] [a-z] s1 s2 _ [0-9]

  10. Simpel float udtryk: -?[0-9]+(\.[0-9]+)? digit digit -1.3 12.345 456 -23 s1 digit . s2 digit s3 s4 digit - . s5 s1 s2 s3 s4 s5 -------------- s5 x x x x | ’-’ x s3 x x s3| ’.’ s2 s2 s4 s4 s2| ’0’ s2 s2 s4 s4 s2| ’1’ ........... | ’2’ • Slutbetingelse: • FSA befinder sig i en sluttilstand • og næste tegn giver X ved opslag. • Fejlbetingelse: • FSA befinder sig ikke i en sluttilstand • og næste tegn giver X ved opslag.

  11. C-program. // ---------------- s1 s2 s3 s4 s5 char state[12][5]= {(s5,-1,-1,-1,-1), // - (-1,s3,-1,-1,s3), // . (s2,s2,s4,s4,s2), // 0 (s2,s2,s4,s4,s2), // 1 (s2,s2,s4,s4,s2), // 2 (s2,s2,s4,s4,s2), // 3 (s2,s2,s4,s4,s2), // 4 (s2,s2,s4,s4,s2), // 5 (s2,s2,s4,s4,s2), // 6 (s2,s2,s4,s4,s2), // 7 (s2,s2,s4,s4,s2), // 8 (s2,s2,s4,s4,s2)}; // 9 char Oldstate= s1, Cstate= s1; char ch= getnextch(); while (ch =! eof) { Cstate = state[ch][Cstate]; ch= getnextch(); if (Cstate < 0) { if ((Oldstate == s2)|| (Oldstate == s4)) break; else{cout<<”Parser fejl”<<endl;exit(0);} } Oldstate= Cstate; } cout << "Parsning OK!" << endl;

  12. lex/flex Et værktøj til opbygning af leksikalsk analyse ud fra regulær grammatik lex.yy.c Fo.l Lex Regulær grammatik samt div. int yylex(); char *yytext: int yylval; int yyleng; FILE *yyin= stdin;

  13. Lex filen: fo.l %{ pre C kode fx #include, #define og var. erklæring. }% Definitioner %% Regler %% Subroutiner • xyz Strengen ’’xyz’’ • x|y strengen x eller y • [0-9] Et tegn mellem 0 og 9 • x* nul eller flere x • x+ Et elle flere x • x? Valgfrit x • ^x x som første tegn på en linie. • x$ x som sidste tegn på en linie. • x/y x men kun hvis det efterfølges af et y • (xy) Gruppering • . Alle tegn undtagen newline • [^x] Komplementærmængden til x • -?(([0-9]+)|([0-9]*\.[0-9]+))([eE][-+]?[0-9]+)?

  14. Simpel: WC %{ int chnr= 0, ordnr= 0, lnr= 0; %} Ord [^ \t\n]+ %% {ord} {ordnr++; chnr+=yyleng;} \n {chnr++; lnr++;} . {chnr++;} %% intmain(){ yylex(); printf("ant.linier: %d, ant.ord: %d, ant.tegn: %d\n", lnr,ordnr,chnr); } > lex wc.l > gcc lex.yy.c –ll –o wc

  15. %{ #include <string.h> #include <stdio.h> #include "symtab.h" #include "rpd.h" %} variabelnavn [a-zA-Z][a-zA-Z0-9]* %% begin {return beginT;} end {return endT;} int {return intT;} float {return floatT;} program {return programT;} {variabelnavn} {if ((yylval.symptr= lookup_sym(yytext)) == NULL) yylval.symptr= insert_sym(yytext); return idT;} [ \t\n] ; . {return yytext[0];} %%

  16. Kædet hash tabel Index= hash(name); ptr name pt name pt NULL ptr name pt NULL ptr NULL ptr name pt NULL ptr NULL ptr NULL ptr name pt name pt name pt NULL ptr NULL symbolrray

  17. void init_sym() { int i; for (i = 0; i < HASHSIZE; i++) symbolarray[i] = NULL; } struct symnote *lookup_sym(char *name) { struct symnote *ptr; int index = hash(name); ptr = symbolarray[index]; while ((ptr != NULL) && (strcmp(ptr->name, name) != 0)) ptr = ptr->pt; return ptr; } struct symnote *insert_sym(char *name) { struct symnote *ptr; int index = hash(name); ptr= (struct symnote *)malloc(sizeof(struct symnote)); ptr->pt= symbolarray[index]; symbolarray[index] = ptr; ptr->name = strdup(name); return ptr; }

More Related