Advertisement
Guest User

Untitled

a guest
Mar 21st, 2019
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.12 KB | None | 0 0
  1. /*
  2.  Si consideri la Sintassi Astratta sotto, per il Lambda Calcolo:
  3.  Lambda::= Var | Const | [$] Var Lambda | [@] Lambda Lambda
  4.  dove usiamo $ per l'astrazione e @ per l'applicazione.
  5.  ....
  6.  
  7.  LastT.c รจ il modulo delle procedure del modulo header, LastT.h, che fornisce
  8.  l'implementazione
  9.  di 5 nuovi tipi:
  10.  VartT, ConstT, AbsT, AppT, LastT
  11.  I tipi ide e num possono essere direttamente implementati con i tipi string
  12.  e int, in analogia a quanto fatto in laboratorio per i ParseTree in C.
  13.  Il tipo LastT introduce alberi astratti il Lambda Calcolo. Questo ipo deve
  14.  contenere 4 distinti tipi di alberi astratti per i diversi tipi di termini
  15.  del Lambda Calcolo.
  16.  
  17.  COMPILAZIONE: Utiizzare opzione syntax del compilatore
  18.         cc Last.c -fsyntax-only
  19.  in altri compilatori, ad es. gcc, controllare nome della option mediante
  20.  comando man, ad es. man gcc.
  21.  */
  22.  
  23. #include <stdio.h>
  24. #include <stdlib.h>
  25. #include <string.h>
  26. #include "Last.h"
  27.  
  28. int main() {
  29.     char X[2], Y[2];
  30.     X[0] = 'x';
  31.     Y[0] = 'y';
  32.  
  33.     LastT s = mkAbs(getIde(mkConst(X)), mkAbs(getIde(mkConst(Y)), mkApp(mkConst(X), mkConst(Y))));
  34.  
  35.     printAST(s);
  36.  
  37.     printf("\n");
  38.  
  39.     return 0;
  40. }
  41.  
  42. //Le operazioni su LastT.c:
  43. // mkConst, mkVar, mkAbs, mkApp, isConst, isVar, isAbs, isApp
  44. // printAST, getIde, getTem, getFun, getArg
  45.  
  46. LastT mkConst(char* c){
  47.     LastT newEl = NULL;
  48.  
  49.     newEl = malloc(sizeof(LastT));
  50.     newEl->flag = Const;
  51.     newEl->term.C.name = c;
  52.  
  53.     return newEl;
  54. }
  55.  
  56. LastT mkVar(char* x){
  57.     LastT newEl = NULL;
  58.  
  59.     newEl = malloc(sizeof(LastT));
  60.     newEl->flag = Var;
  61.     newEl->term.V.name = x;
  62.  
  63.     return newEl;  
  64. }
  65.  
  66. LastT mkAbs(char* x, LastT t){
  67.     LastT newEl = NULL;
  68.  
  69.     newEl = malloc(sizeof(LastT));
  70.     newEl->flag = Abs;
  71.     newEl->term.A.name = x;
  72.     newEl->term.A.term = t;
  73.  
  74.     return newEl;  
  75. }
  76.  
  77. LastT mkApp(LastT t1, LastT t2){
  78.     LastT newEl = NULL;
  79.  
  80.     newEl = malloc(sizeof(LastT));
  81.     newEl->flag = App;
  82.     newEl->term.P.fun = t1;
  83.     newEl->term.P.arg = t2;
  84.  
  85.     return newEl;
  86. }
  87.  
  88. bool isConst(LastT t){
  89.     return (t->flag == Const);
  90. }
  91.  
  92. bool isVar(LastT t){
  93.     return (t->flag == Var);
  94. }
  95.  
  96. bool isAbs(LastT t){
  97.     return (t->flag == Abs);
  98. }
  99.  
  100. bool isApp(LastT t){
  101.     return (t->flag == App);
  102. }
  103.  
  104. void printAST(LastT t){
  105.     printf("[");
  106.    
  107.     if (isConst(t) || isVar(t))
  108.         printf("%c", getIde(t)[0]);
  109.  
  110.     else if (isAbs(t)) {
  111.         printf("$-([%c],", getIde(t)[0]);
  112.         printAST (getTerm(t));
  113.         printf(")");
  114.     }
  115.  
  116.     else if (isApp(t)) {
  117.         printf("@-(");
  118.         printAST(getFun(t));
  119.         printf(",");
  120.         printAST(getArg(t));
  121.         printf(")");
  122.     }
  123.  
  124.     printf("]");
  125. }
  126.  
  127. char* getIde(LastT t){
  128.     if (isConst(t))
  129.         return t->term.C.name;
  130.     else if (isVar(t))
  131.         return t->term.V.name;
  132.     else if (isAbs(t))
  133.         return t->term.A.name;
  134. }
  135.  
  136. LastT getTerm(LastT t){
  137.     if (isAbs(t))
  138.         return t->term.A.term;
  139. }
  140.  
  141. LastT getFun(LastT t){
  142.     if (isApp(t))
  143.         return t->term.P.fun;
  144. }
  145.  
  146. LastT getArg(LastT t){
  147.     if (isApp(t))
  148.         return t->term.P.arg;
  149. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement