1 / 17

Sprog og grammatik

Sprog og grammatik. Et sprog L(G) er mængden af sætninger afledt af grammatikken G. Grammatik er en 4-tupel: G={N, S ,P,S}. Hvor: N er et sæt af non-terminale symboler S er et sæt af terminale symboler P er et sæt af produktioner

chidi
Télécharger la présentation

Sprog og grammatik

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. Sprog og grammatik Et sprog L(G) er mængden af sætninger afledt af grammatikken G. Grammatik er en 4-tupel: G={N,S,P,S}. Hvor:N er et sæt af non-terminale symboler Ser et sæt af terminale symboler P er et sæt af produktioner S er sætnings- eller startsymbolet.

  2. Grammatik på BNF form Grammastart -> program ( ident) globalvar begin statementlist end ident ->              letter tegenlist letter ->             a | b | c | d | ........ | å | A | B | ...... | Å tegnlist ->         tegn tegnlist | e tegn ->              letter | digit digit ->              0 | 1 | 2 | 3 | ...... | 9 globalvar->      type identlist; type ->               char | int | float identlist ->          ident | ident , identlist statementlist->   statementstatementlist| e statement ->        ident=exp ; exp ->                   exp + exp | exp - exp | exp * exp | exp / exp | (exp) | ident

  3. Backus-Naur form <Grammastart> ::= "program" "(" <ident> ")" <globalvar> "begin" <statementlist> "end” <globalvar> ::=  <type> <identlist> ";” <type> ::=  "char" | "int" | "float"

  4. EBNF En variation af BNF med nogle ekstra elementer. [..] List af alternative elementer. * En sekvens af nul eller flere elementer. + En sekvens af en eller flere elementer. (..) En gruppe af elementer. BNF: <funklist>::= <funktion> <funklist> | e EBNF: <funklist>::= (<funktion>)*

  5. Grammatik typer: Type 0 grammatik:Fri grammatik Type 1 grammatik:Context−sensitiv grammatik Type 2 grammatik: Context−fri grammatik Type 3 grammatik:Finit grammatik

  6. Grammatik typer: a A b -> a g b A -> g A -> a A -> a B A -> B a

  7. Parser træer Parsning er processen at aflede en sætning fra et startsymbol ved gentagende anvendelse af produktioner S -> AB A -> A x | y B -> z Sætning: yxxz S A B A A z y x x

  8. Parser træer program->stlist stlist-> statement stlist | e statement-> idT = exp ; exp -> exp + exp | exp – exp | exp * exp | exp / exp | intT | idT Sprog: v= 8; k= 3+5-7*v;

  9. Left-most : Right-most afledning Right−most parsing udfolder parsertræet fra højre. Left−most parsning udfolder parsertræet fra venstre. S -> A B Left−most Right−most

  10. Ambiguous (flertydig) grammatik Flere parsertræer for samme sætning: S -> A A A -> x | xx Sætning: xxx S S A A A A x x x x x x Flertydig mening Ikke konsistent compiler

  11. Ækvivalent grammatik Samme sprog men ikke samme grammatik L(G) = L(G’) : G: A -> A x | y G’: A -> x B B -> xB |e A A A B A x x y x B y e x Forskellig mening.

  12. Rekurtion Venstre rekursiv: A -> u | A v Fx: funklist-> funklist funktion | e Højre rekursiv: A -> u | v A Fx: funklist-> funktion funklist | e exp -> exp + exp | exp – exp | exp * exp | exp / exp Både højre og venstre rekursiv.

  13. Rekurtion: Venstre til højre Venstre rekursiv: A -> u | A v Kan omskrives til: A -> u B B -> v B | e

  14. Precedence og flertydig grammatik exp -> exp + exp | exp – exp | exp * exp | exp / exp | ( exp ) | intT | idT Sætning: 2+3*4 exp exp exp * exp exp + exp exp + exp exp * exp intT intT intT intT intT intT 4 2 3 4 2 3

  15. Korrekt precedence exp->  term expBexpB->          termopr term expB | etermopr-> + |- term->            factor termBtermB->          factoropr factor termB | e factoropr-> * | / factor->          uopr exp | ( exp ) | intT | idT uopr->         - | e

  16. statement IdT = exp ; term + term factor factor * factor ( exp ) IdT IntT term + term a 3 factor factor IdT ( exp ) b term - term factor factor IdT IdT Korrekt precedence: EBNF <exp> ::= <term>(( “+” | “-” ) <term>)* <term> ::= <factor>(( “*” | “/” ) <factor>)* <Factor>::=  <uopr> <exp>              | ”(“ <exp> “)”             |  <intT>              |  <idT><uopr>::=    “-” | e A=(b + (c - c)) + a * 3; c c

  17. Left factoring A -> v w | v z Kan omskrives til: A -> v B B -> w | z

More Related