Download presentation
Presentation is loading. Please wait.
Published byDinah Cummings Modified over 9 years ago
1
by Neng-Fa Zhou Programming language syntax 4 Three aspects of languages –Syntax How are sentences formed? –Semantics What does a sentence mean? –Pragmatics How to use the language? 4 Only syntax can be described formally –Regular expressions and context-free grammars
2
by Neng-Fa Zhou Regular expressions (RE) The empty string is a RE Every symbol in (alphabet) is a RE 4 Let r and s be REs. –r | s: or –rs: concatenation –(r) * : zero or more instances –(r) + : one or more instances –(r)?: zero or one instance
3
by Neng-Fa Zhou Precedence of operators high low r* r + r? rs r|s all left associative 4 Examples = {a,b} 1. a|b 2. (a|b)(a|b) 3. a* 4. (a|b)* 5. a| a*b
4
by Neng-Fa Zhou Algebraic Properties of RE
5
by Neng-Fa Zhou d 1 r 1 d 2 r 2 d n r n.... d i is a RE over {d 1,d 2,...,d i-1 } Regular Definitions not recursive
6
by Neng-Fa Zhou Examples 4 Identifiers 4 Decimal integers in Java 4 Hexadecimal integers letter A | B |... | Z | a | b |... | z digit 0 | 1 |... | 9 id letter ( letter | digit )* DecimalNumeral 0 | nonZeroDigit digit* HexaNumeral (0x | 0X) hexadigit+
7
by Neng-Fa Zhou Lex 4 A tool for automatically generating lexical analyzers
8
by Neng-Fa Zhou Lex Specifications declarations % translation rules % auxiliary procedures p 1 {action 1 } p 2 {action 2 }... p n {action n }
9
by Neng-Fa Zhou Lex Regular Expressions
10
Example-1 by Neng-Fa Zhou %{ int num_lines = 0, num_chars = 0; %} % \n ++num_lines; ++num_chars;. ++num_chars; % main() { yylex(); printf( "# of lines = %d, # of chars = %d\n", num_lines, num_chars ); } yywrap(){return 0;}
11
by Neng-Fa Zhou Example-2 D [0-9] INT {D}{D}* % {INT}("."{INT}((e|E)("+"|-)?{INT})?)? {printf("valid %s\n",yytext);}. {printf("unrecognized %s\n",yytext);} % int main(int argc, char *argv[]){ ++argv, --argc; if (argc>0) yyin = fopen(argv[0],"r"); else yyin = stdin; yylex(); } yywrap(){return 0;}
12
java.util.regex by Neng-Fa Zhou import java.util.regex.*; class Number { public static void main(String[] args){ String regExNum = "\\d+(\\.\\d+((e|E)(\\+|-)?\\d+)?)?"; if (Pattern.matches(regExNum,args[0])) System.out.println("valid"); else System.out.println("invalid"); }
13
String Pattern Matching in Perl by Neng-Fa Zhou print "Input a string :"; $_ = ; chomp($_); if (/^[0-9]+(\.[0-9]+((e|E)(\+|-)?[0-9]+)?)?$/){ print "valid\n"; } else { print "invalid\n"; }
14
by Neng-Fa Zhou Context-free Grammars – is a finite set of terminals – N is a finite set of non-terminals – P is a finite subset of production rules – S is the start symbol G=( ,N,P,S)
15
by Neng-Fa Zhou E T | E + T | E - T T F | T * F |T / F F id | (E) CFG: Examples 4 Arithmetic expressions 4 Statements IfStatement if E then Statement else Statement
16
by Neng-Fa Zhou CFG vs. Regular Expressions 4 CFG is more expressive than RE –Every language that can be described by regular expressions can also be described by a CFG 4 Example languages that are CFG but not RE –if-then-else statement, {a n b n | n>=1} 4 Non-CFG –L1={wcw | w is in (a|b)*} –L2={a n b m c n d m | n>=1 and m>=1}
17
by Neng-Fa Zhou Derivations if andthen * ** S * is a sentential form is a sentence if it contains only terminal symbols
18
by Neng-Fa Zhou Derivations 4 leftmost derivation 4 Rightmost derivation if is a string of terminals if is a string of terminals
19
by Neng-Fa Zhou Parse Trees 4 A parse tree is any tree in which –The root is labeled with S –Each leaf is labeled with a token a or –Each interior node is labeled by a nonterminal –If an interior node is labeled A and has children labeled X1,.. Xn, then A X1...Xn is a production.
20
by Neng-Fa Zhou Parse Trees and Derivations E E + E | E * E | E - E | - E | ( E ) | id
21
by Neng-Fa Zhou YACC %token DIGIT % lines: lines expr '\n'{printf("%d\n",$2);} | lines '\n' | ; expr: expr '+' term{$$ = $1 + $3;} | expr '-' term{$$ = $1 - $3;} | term ; term: term '*' factor {$$ = $1 * $3;} | term '/' factor {$$ = $1 / $3;} | factor ; factor : '(' expr ')'{$$ = $2;} | DIGIT ; %
22
DCG in Prolog Strings with an equal number of 0’s and 1’s 4 DCG 4 Prolog clauses by Neng-Fa Zhou :-table e/2. e --> []. e --> [0],e,[1]. e --> [1],e,[0]. e --> e,e. :-table e/2. e(A, A). e(A, B) :- 'C'(A, 0, C), e(C, D), 'C'(D, 1, B). e(A, B) :- 'C'(A, 1, C), e(C, D), 'C'(D, 0, B). e(A, B) :- e(A, C), e(C, B).
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.