Advertisement
sanpai

Recursive predictive parser for function protoype (incomplet

Mar 31st, 2013
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.00 KB | None | 0 0
  1. #include<stdio.h>
  2.  
  3. FILE *fp;
  4. void scan();
  5. void error();
  6.  
  7. void retcheck();
  8. void name();
  9. void param();
  10.  
  11.  
  12.  
  13. int main()
  14. {
  15. fp=fopen("newinput.txt","r+");
  16. scan();
  17. }
  18.  
  19.  
  20. void scan()
  21. {
  22. char ch;
  23.  
  24. ch=fgetc(fp);
  25.  
  26.  
  27. if((ch>=65&&ch<=90)||(ch>=97&&ch<=122))
  28. {
  29.  
  30. retcheck();
  31. }
  32.  
  33. else
  34. {
  35. error();
  36. }
  37.  
  38. }
  39.  
  40.  
  41. void error()
  42. {
  43. printf("\n Error in prototype declaration ");
  44.  
  45. fclose(fp);
  46. }
  47.  
  48.  
  49. void retcheck()
  50. {
  51.  
  52. fseek(fp,-1,SEEK_CUR);
  53. char ch;
  54.  
  55. ch=fgetc(fp);
  56.  
  57. if(ch=='i')
  58. {
  59.  
  60. ch=fgetc(fp);
  61. if(ch=='n')
  62. {
  63. ch=fgetc(fp);
  64.  
  65.  
  66. if(ch=='t')
  67. {
  68. printf("\n Return Type is int ");
  69. name();
  70. }
  71.  
  72. else
  73. {
  74. error();
  75. }
  76. }
  77.  
  78. else
  79. {
  80. error();
  81. }
  82. }
  83.  
  84.  
  85. else if(ch=='f')
  86. {
  87.  
  88. ch=fgetc(fp);
  89. if(ch=='l')
  90. {
  91.  
  92. ch=fgetc(fp);
  93.  
  94. if(ch=='o')
  95. {
  96. ch=fgetc(fp);
  97. if(ch=='a')
  98. {
  99. ch=fgetc(fp);
  100. if(ch=='t')
  101. {
  102. printf("\n The return type is float \n");
  103. name();
  104. }
  105.  
  106. else
  107. {
  108. error();
  109. }
  110. }
  111.  
  112. else
  113. error();
  114. }
  115. else
  116. error();
  117.  
  118. }
  119. else
  120. error();
  121.  
  122. }
  123.  
  124.  
  125.  
  126. else if(ch=='s')
  127. {
  128.  
  129. ch=fgetc(fp);
  130. if(ch=='t')
  131. {
  132. ch=fgetc(fp);
  133.  
  134. if(ch=='r')
  135. {
  136. ch=fgetc(fp);
  137.  
  138. if(ch=='u')
  139. {
  140. ch=fgetc(fp);
  141.  
  142. if(ch=='c')
  143. {
  144. ch=fgetc(fp);
  145.  
  146. if(ch=='t')
  147. {
  148. printf("\n The return type is struct ");
  149. name();
  150. }
  151. else
  152. {
  153. error();
  154. }
  155.  
  156.  
  157.  
  158. }
  159. else
  160. error();
  161.  
  162. }
  163. else
  164. error();
  165.  
  166. }
  167. else
  168. error();
  169. }
  170.  
  171. else
  172. error();
  173. }
  174.  
  175. else
  176. {
  177. error();
  178. }
  179.  
  180.  
  181. }
  182.  
  183. void name()
  184. {
  185. char ident[10],ch;
  186.  
  187. int i=0;
  188.  
  189. ch=fgetc(fp);
  190. if(ch==' '||ch=='\t')
  191. {
  192. do
  193. {
  194. ident[i]=ch;
  195. ++i;
  196. ch=fgetc(fp);
  197. }while((ch>=65 && ch<=90)||(ch>=97 & ch<=122)||(ch>=48 & ch<=57)); //keep scanning
  198.  
  199.  
  200. ident[i]='\0';
  201.  
  202. }
  203.  
  204. else
  205. {
  206. error();
  207. }
  208.  
  209. printf("\n %s is the function name ",ident);
  210.  
  211. fseek(fp,-1,SEEK_CUR);
  212. param();
  213.  
  214.  
  215. }
  216.  
  217.  
  218. void param()
  219. {
  220. char ch;
  221. ch=fgetc(fp);
  222.  
  223. if(ch=='(')
  224. {
  225.  
  226. }
  227.  
  228. else
  229. error();
  230. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement