A FRENCH-TO-JAVANESE TRANSLATER.
[moreno@iguanodon lex]$ more src/french2javanese.l %s CR VOWEL [Aa]|[Ee]|[Ii]|[Oo]|[Uu]|[Yy] CONSONANT [B-Db-dF-Hf-hJ-Nj-nP-Tp-tV-Xv-xZz] %% <CR>{VOWEL} {BEGIN 0; printf("%s","av"); ECHO;} {CONSONANT} {ECHO; BEGIN CR;} %% [moreno@iguanodon lex]$ make french2javanese.out [moreno@iguanodon lex]$ ./bin/french2javanese Bonjour chers etudiants Bavonjavour chavers avetavudaviants Est-ce que le cours est interessant aujourd'hui ? avEst-cave qavue lave cavours avest avintaveravessavant avaujavourd'havui ?Observe that the last line above starts with avEst. Can you explain why ?
AN ENGLISH-TO-FRENCH TRANSLATER FOR THE PASCAL LANGUAGE. Our aim is also to
[moreno@iguanodon lex]$ more test/pascal2pascal.test program p; var i,j : integer; begin i := 23; j := i + i; end. [moreno@iguanodon lex]$ more src/pascal2pascal.l %{ /* need this for the call to atof() below */ #include <math.h> #include <stdio.h> FILE *fic; %} PLUS [\+] MINUS [\-] TIMES [\*] BY [/] ASSIGN := BINARYOP {PLUS}|{MINUS}|{TIMES}|{BY}|{ASSIGN} SEPARATOR [,:;\.] PROGRAM [Pp][Rr][Oo][Gg][Rr][Aa][Mm] VAR [Vv][Aa][Rr] IF [Ii][Ff] THEN [Tt][Hh][Ee][Nn] ELSE [Ee][Ll][Ss][Ee] BEGIN [Bb][Ee][Gg][Ii][Nn] END [Ee][Nn][Dd] WHILE [Ww][Hh][Ii][Ll][Ee] FOR [Ff][Oo][Rr] DO [Dd][Oo] TO [Tt][Oo] FUNCTION [Ff][Uu][Nn][Cc][Tt][Ii][Oo][Nn] PROCEDURE [Pp][Rr][Oo][Cc][Ee][Dd][Uu][Rr][Ee] LETTER [A-Za-z] DIGIT [0-9] ID {LETTER}({LETTER}|{DIGIT})* INTEGER ({PLUS}|{MINUS})?{DIGIT}+ REAL {INTEGER}(\.({DIGIT})*)?([eE]{INTEGER})? COMMENT "{"[^}\n]*"}" %% {INTEGER} { printf( "An integer: %s (%d)\n", yytext, atoi( yytext ) ); fprintf(fic, "%d", atoi(yytext)); } {REAL} { printf( "A float: %s (%g)\n", yytext, atof( yytext ) ); fprintf(fic, "%g", atof(yytext)); } {IF} convert_IF(); {THEN} convert_THEN(); {ELSE} convert_ELSE(); {BEGIN} convert_BEGIN(); {END} convert_END(); {VAR} convert_VAR(); {FOR} convert_FOR(); {DO} convert_DO(); {TO} convert_TO(); {WHILE} convert_WHILE(); {PROCEDURE} convert_PROCEDURE(); {FUNCTION} convert_FUNCTION(); {PROGRAM} convert_PROGRAM(); {ID} { printf( "An identifier: %s\n", yytext ); fprintf(fic, "%s", yytext ); } {SEPARATOR} { printf( "A separator: %s\n", yytext ); fprintf(fic, "%s", yytext ); } {BINARYOP} { printf( "A binary operator: %s\n", yytext ); fprintf(fic, "%s", yytext ); } {COMMENT} { printf( "A comment : %s\n", yytext ); fprintf(fic, "%s", yytext ); } ^[ \t]+ fprintf(fic, "%s", yytext ); [ \t]+ fprintf(fic, "%s", " "); [\n]+ fprintf(fic, "%s", "\n"); . printf( "Unrecognized character: %s\n", yytext ); %% keyword_message() { printf( "A keyword: %s\n", yytext );} convert_IF() { keyword_message(); fprintf(fic, "%s", "SI"); } convert_THEN() { keyword_message(); fprintf(fic, "%s", "ALORS"); } convert_ELSE() { keyword_message(); fprintf(fic, "%s", "SINON"); } convert_BEGIN() { keyword_message(); fprintf(fic, "%s", "DEBUT") ; } convert_END() { keyword_message(); fprintf(fic, "%s", "FIN") ; } convert_VAR() { keyword_message(); fprintf(fic, "%s", "VAR") ; } convert_FOR() { keyword_message(); fprintf(fic, "%s", "POUR") ; } convert_DO() { keyword_message(); fprintf(fic, "%s", "FAIRE") ; } convert_TO() { keyword_message(); fprintf(fic, "%s", "A") ; } convert_WHILE() { keyword_message(); fprintf(fic, "%s", "TANT QUE") ; } convert_PROCEDURE() { keyword_message(); fprintf(fic, "%s", "PROCEDURE") ; } convert_FUNCTION() { keyword_message(); fprintf(fic, "%s", "FONCTION") ; } convert_PROGRAM() { keyword_message(); fprintf(fic, "%s", "PROGRAMME") ; } int main() { fic = fopen("/tmp/pascal","w"); yylex(); fclose(fic); return 0; }
[moreno@iguanodon lex]$ more test/pascal2pascal.test program p; var i,j : integer; begin i := 23; j := i + i; end. moreno@iguanodon lex]$ ./bin/pascal2pascal < test/pascal2pascal.test A keyword: program An identifier: p A separator: ; A keyword: var An identifier: i A separator: , An identifier: j A separator: : An identifier: integer A separator: ; A keyword: begin n identifier: i A binary operator: := An integer: 23 (23) A separator: ; An identifier: j A binary operator: := An identifier: i A binary operator: + An identifier: i A separator: ; A keyword: end A separator: . [moreno@iguanodon lex]$ more /tmp/pascal PROGRAMME p; VAR i,j : integer; DEBUT i := 23; j := i + i; FIN.
A SCANNER FOR C/JAVA SOURCE FILES We aim to count the number of
[moreno@iguanodon lex]$ more src/C_source_scanner.l %{ int comments = 0, code = 0, whiteSpace = 0; %} %s CS COMMENT "/*".*"*/" COMMENTLINE ^[ \t]*{COMMENT}[ \t]*\n WHITELINE ^[ \t]*\n CODESTARTINGLINE .+({COMMENT}.+)*.*\n CODEENDINGLINE .*({COMMENT}.+)*.+\n CODELINE {CODESTARTINGLINE}|{CODEENDINGLINE} STARTCOMMENTLINE ^[ \t]*"/*" %% {COMMENTLINE} {comments++; /* comment on a single line */ } {WHITELINE} {whiteSpace++;} {CODELINE} {code++;} {STARTCOMMENTLINE} {BEGIN CS; /* enter comment eating state */ } <CS>"*/" {BEGIN 0;} <CS>\n {comments++ ;} %% main() { yylex(); printf("code: %d, comments: %d, white space: %d\n", code, comments, whiteSpace); return 0; } [moreno@iguanodon lex]$ make C_source_scanner.out [moreno@iguanodon lex]$ ./bin/C_source_scanner < src/C_source_scanner.l code: 26, comments: 2, white space: 8 [moreno@iguanodon lex]$