1 / 54

ecs30 Winter 2012: Programming and Problem Solving # 21: Flex / Bison, JSON, Files

ecs30 Winter 2012: Programming and Problem Solving # 21: Flex / Bison, JSON, Files. Prof . S. Felix Wu Computer Science Department University of California, Davis http://dsl.ucdavis.edu/~wu/ecs30/.

dysis
Download Presentation

ecs30 Winter 2012: Programming and Problem Solving # 21: Flex / Bison, JSON, Files

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. ecs30 Winter 2012:Programming and Problem Solving#21: Flex/Bison, JSON, Files Prof . S. Felix Wu Computer Science Department University of California, Davis http://dsl.ucdavis.edu/~wu/ecs30/ ecs30 Winter 2012 Lecture #20

  2. {"id":"204722549606084_262650593813279","from":{"name":"Corey Hobbs","id":"100001896707606"},"to\ ":{"data":[{"version":1,"name":"ecs30 Programming and Problem Solving","id":"204722549606084"}]}\ ,"message":"Just some ECS30 humor to lighten your Day! :)","picture":"http:\/\/photos-a.ak.fbcdn\ .net\/hphotos-ak-snc7\/429242_281418948597993_100001896707606_668040_1257901773_s.jpg","link":"h\ ttp:\/\/www.facebook.com\/photo.php?fbid=281418948597993&set=o.204722549606084&type=1","icon":"h\ ttp:\/\/static.ak.fbcdn.net\/rsrc.php\/v1\/yz\/r\/StEh3RhPvjk.gif","actions":[{"name":"Comment",\ "link":"http:\/\/www.facebook.com\/204722549606084\/posts\/262650593813279"},{"name":"Like","lin\ k":"http:\/\/www.facebook.com\/204722549606084\/posts\/262650593813279"}],"type":"photo","object\ _id":"281418948597993","created_time":"2012-03-02T16:37:27+0000","updated_time":"2012-03-02T16:4\ 5:18+0000","likes":{"data":[{"name":"Peter D. Lai","id":"633444184"}],"count":2},"comments":{"da\ ta":[{"id":"204722549606084_262650593813279_262652393813099","from":{"name":"Matt Song","id":"76\ 7960581"},"message":"Already made a meme :D\nhttps:\/\/fbcdn-sphotos-a.akamaihd.net\/hphotos-ak-\ snc7\/s720x720\/418154_10151339744545582_767960581_22979633_746789299_n.jpg","created_time":"201\ 2-03-02T16:41:17+0000","likes":2},{"id":"204722549606084_262650593813279_262654177146254","from"\ :{"name":"Corey Hobbs","id":"100001896707606"},"message":"-___-","created_time":"2012-03-02T16:4\ 5:15+0000"},{"id":"204722549606084_262650593813279_262654200479585","from":{"name":"Corey Hobbs"\ ,"id":"100001896707606"},"message":"i saw... lol","created_time":"2012-03-02T16:45:18+0000"}],"c\ ount":3},"is_published":true} ecs30 Winter 2012 Lecture #20

  3. Object ecs30 Winter 2012 Lecture #20

  4. Array ecs30 Winter 2012 Lecture #20

  5. STRING ecs30 Winter 2012 Lecture #20

  6. Value ecs30 Winter 2012 Lecture #20

  7. Number ecs30 Winter 2012 Lecture #20

  8. FLEX/BISON ecs30 Winter 2012 Lecture #20

  9. General Compiler Infra-structure Syntactic Structure Program source (stream of characters) Tokens Parser Semantic Routines Scanner (tokenizer) Lex Yacc IR: Intermediate Representation (1) Analysis/ Transformations/ optimizations Symbol and Attribute Tables IR: Intermediate Representation (2) Code Generator Assembly code

  10. Lex & Yacc • Lex • generates C code for the lexical analyzer (scanner) • Token patterns specified by regular expressions • Yacc • generates C code for a LR(1) syntax analyzer (parser) • BNF rules for the grammar

  11. Lex program structure %{ #include <stdio.h> #include "y.tab.h" int c; extern int yylval; %} %% " " ; [a-z] { c = yytext[0]; yylval = c - 'a'; return(LETTER); } [0-9]* { yylval = atoi(yytext); return(NUMBER); } [^a-z0-9\b] { c = yytext[0]; return(c); } … definitions … %% … rules … %% … subroutines …

  12. Yacc Program Structure … definitions … %% … rules … %% … subroutines … %{ #include <stdio.h> int regs[26]; int base; %} %token NUMBER LETTER %left '+' '-‘ %left '*' '/‘ %% list: | list stat '\n' |list error '\n' {yyerrok;} ; stat: expr {printf("%d\n",$1);} | LETTER '=' expr {regs[$1] = $3;}; expr: '(' expr ')'   {$$ = $2;} | expr '+' expr {$$ = $1 + $3;} | LETTER {$$ = regs[$1];} %% main(){return(yyparse());} yyerror(CHAR *s){fprintf(stderr, "%s\n",s);} yywrap(){ return(1);}

  13. fwrite/fread • format versus binary #include <stdio.h> size_t fread(void *restrict ptr, size_t size, size_t nitems, FILE *restrict stream); size_t fwrite(const void *restrict ptr, size_t size, size_t nitems, FILE *restrict stream); ecs30b Fall 2008 Lecture #26

  14. argv_test.c #include <stdio.h> #include <stdlib.h> #include <strings.h> #define MAX_NAME 8192 int main (int argc, char *argv[]) // argv is an array of strings { int i; fprintf(stderr, "totally, we have %d arguments\n", argc); for (i = 0; i < argc; i++) { fprintf(stderr, "the #%2d argument is [%20s].\n", i, argv[i]); } return 1; } ecs30b Fall 2008 Lecture #26

  15. #define STRSIZ 10 typedef struct { char name[STRSIZ]; double diameter; int moons; double orbit_time, rotation_time; } planet_t; … planet_t current_planet[12]; fwrite(current_planet, sizeof(planet_t), 12, planetFile); fread(current_planet, sizeof(planet_t), 12, planetFile); ecs30b Fall 2008 Lecture #26

  16. ecs30b Fall 2008 Lecture #26

  17. typedef struct { double diameter; int moons; char name[10]; double orbit_time, rotation_time; char tail[2]; } planet_t; sizeof(planet_t) == ? ecs30b Fall 2008 Lecture #26

  18. typedef struct { double diameter; int moons; char name[10]; double orbit_time, rotation_time; char tail[2]; char tail2; } planet_t; sizeof(planet_t) == ? ecs30b Fall 2008 Lecture #26

  19. fwrite/fread Table 12.5 Pages 625~626 • format versus binary #include <stdio.h> size_t fread(void *restrict ptr, size_t size, size_t nitems, FILE *restrict stream); size_t fwrite(const void *restrict ptr, size_t size, size_t nitems, FILE *restrict stream); ecs30b Fall 2008 Lecture #26

  20. /usr/include/stdio.h typedef struct __sFILE { unsigned char *_p; /* current position in (some) buffer */ int _r; /* read space left for getc() */ int _w; /* write space left for putc() */ short _flags; /* flags, below; this FILE is free if 0 */ short _file; /* fileno, if Unix descriptor, else -1 */ struct __sbuf _bf;/* the buffer (at least 1 byte, if !NULL) */ int _lbfsize; /* 0 or -_bf._size, for inline putc */ /* operations */ void *_cookie; /* cookie passed to io functions */ int (*_close)(void *); int (*_read) (void *, char *, int); fpos_t (*_seek) (void *, fpos_t, int); int (*_write)(void *, const char *, int); /* separate buffer for long sequences of ungetc() */ struct __sbuf _ub; /* ungetc buffer */ … } FILE; ecs30b Fall 2008 Lecture #26

  21. malloc & free • malloc: • Input: the size of the memory cells • Output: the address of the first memory cell ecs30b Fall 2008 Lecture #26

  22. malloc & free • malloc: • Input: the size of the memory cells • return: the address of the first memory cell #include <stdlib.h> void *xp; xp = malloc(sizeof(planet_t)); ecs30b Fall 2008 Lecture #26

  23. malloc & free • free: • Input: an address • return: nothing #include <stdlib.h> void *xp; xp = malloc(sizeof(planet_t)); free(xp); ecs30b Fall 2008 Lecture #26

  24. ecs30b Fall 2008 Lecture #26

  25. NAME strdup -- save a copy of a stringLIBRARY Standard C Library (libc, -lc)SYNOPSIS #include <string.h> char * strdup(const char *s1);DESCRIPTION The strdup() function allocates sufficient memory for a copy of the string s1, does the copy, and returns a pointer to it. The pointer may subsequently be used as an argument to the function free(3). If insufficient memory is available, NULL is returned and errno is set to ENOMEM. ecs30b Fall 2008 Lecture #26

  26. malloc & free • malloc: • Input: the size of the memory cells • Output: the address of the first memory cell ecs30b Fall 2008 Lecture #27

  27. malloc & free • malloc: • Input: the size of the memory cells • return: the address of the first memory cell #include <stdlib.h> void *xp; xp = malloc(sizeof(planet_t)); ecs30b Fall 2008 Lecture #27

  28. malloc & free • free: • Input: an address • return: nothing #include <stdlib.h> void *xp; xp = malloc(sizeof(planet_t)); free(xp); ecs30b Fall 2008 Lecture #27

  29. ecs30b Fall 2008 Lecture #27

  30. int *ip; ip = (int *) malloc(sizeof(int));; ip ecs30b Fall 2008 Lecture #27

  31. int *ip; ip = (int *) malloc(sizeof(int)); ip ecs30b Fall 2008 Lecture #27

  32. int *ip; ip = (int *) malloc(sizeof(int)); double *dp; dp = (double *) malloc(sizeof(double)); ecs30b Fall 2008 Lecture #27

  33. planet_t *ptp; ptp = (planet_t *) malloc(sizeof(planet_t)); ecs30b Fall 2008 Lecture #27

  34. Game of Life int Array[25][25]; int **a_ptr = NULL; n = 25; a_ptr = (int **) malloc(sizeof(int) * n * n); ecs30b Fall 2008 Lecture #27

  35. NAME strdup -- save a copy of a stringLIBRARY Standard C Library (libc, -lc)SYNOPSIS #include <string.h> char * strdup(const char *s1);DESCRIPTION The strdup() function allocates sufficient memory for a copy of the string s1, does the copy, and returns a pointer to it. The pointer may subsequently be used as an argument to the function free(3). If insufficient memory is available, NULL is returned and errno is set to ENOMEM. ecs30b Fall 2008 Lecture #27

  36. #include <stdio.h> #include <string.h> int main(void) { char s1[9] = “UC “; char s2[9] = “Davis”; fprintf(stdout, “%s\n”, strcpy(s1, s2)); return 0; } ecs30b Fall 2008 Lecture #27

  37. ‘U’ ‘C’ ‘ ’ ‘\0’ ? ? ? ? ? s1 ‘D’ ‘a’ ‘v’ ‘i’ ‘s’ ‘\0’ ? ? ? s2 ‘D’ ‘a’ ‘v’ ‘i’ ‘s’ ‘\0’ ? ? ? s1 strcpy(s1, s2); ecs30b Fall 2008 Lecture #27

  38. #include <stdio.h> #include <string.h> int main(void) { char *s1p; char s2[9] = “Davis”; s1p = strdup(s2); fprintf(stdout, “%s\n”, s1p); return 0; } ecs30b Fall 2008 Lecture #27

  39. s1p ‘D’ ‘a’ ‘v’ ‘i’ ‘s’ ‘\0’ ? ? ? s2 ecs30b Fall 2008 Lecture #27

  40. s1p ‘D’ ‘a’ ‘v’ ‘i’ ‘s’ ‘\0’ ? ? ? s2 ? ? ? ? ? ? ecs30b Fall 2008 Lecture #27

  41. s1p ‘D’ ‘a’ ‘v’ ‘i’ ‘s’ ‘\0’ ? ? ? s2 ‘D’ ‘a’ ‘v’ ‘i’ ‘s’ ‘\0’ ecs30b Fall 2008 Lecture #27

  42. char * strdup(char *s) { … } NAME strdup -- save a copy of a stringLIBRARY Standard C Library (libc, -lc)SYNOPSIS #include <string.h> char * strdup(const char *s1);DESCRIPTION The strdup() function allocates sufficient memory for a copy of the string s1, does the copy, and returns a pointer to it. The pointer may subsequently be used as an argument to the function free(3). If insufficient memory is available, NULL is returned and errno is set to ENOMEM. ecs30b Fall 2008 Lecture #27

  43. char * strdup(char *s) { return strcpy( (char *) malloc(sizeof(char) *(strlen(s) + 1)), s); } ecs30b Fall 2008 Lecture #27

  44. Abstractive Data Structures • Array • Stack • Queue • Linked-List ecs30b Fall 2008 Lecture #27

  45. Linked-List struct ecs30b Fall 2008 Lecture #27

  46. Linked-List struct struct struct ecs30b Fall 2008 Lecture #27

  47. Linked-List struct struct struct NULL NULL ecs30b Fall 2008 Lecture #27

  48. Linked-List struct struct struct NULL NULL struct ecs30b Fall 2008 Lecture #27

  49. Linked-List struct struct struct NULL NULL struct ecs30b Fall 2008 Lecture #27

  50. Linked-List struct struct NULL NULL ecs30b Fall 2008 Lecture #27

More Related