aboutsummaryrefslogtreecommitdiff
path: root/5.19.c
diff options
context:
space:
mode:
authorsinanmohd <sinan@firemail.cc>2023-04-10 07:45:35 +0530
committersinanmohd <sinan@firemail.cc>2023-04-10 07:45:35 +0530
commitc14dab7f3a1cf72619fadce9b81b55426dc0b32e (patch)
tree608080198e6e07c683489644623e2770af6fc69c /5.19.c
parent3594391322954f6538087b1af480cf00fd8da2c8 (diff)
5.19: initial commit, make use of makefiles and split main into multiple files
Diffstat (limited to '5.19.c')
-rw-r--r--5.19.c144
1 files changed, 144 insertions, 0 deletions
diff --git a/5.19.c b/5.19.c
new file mode 100644
index 0000000..8709df7
--- /dev/null
+++ b/5.19.c
@@ -0,0 +1,144 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <ctype.h>
+
+#define MAXTOKEN 100
+#define OUTLEN 1000
+#define MAXBUFF 100
+
+enum { NAME, PARENS, BRACKETS };
+enum { NO, YES };
+
+void err(char *s);
+
+int gettoken(void);
+int peaktoken(void);
+char tokentype;
+char token[MAXTOKEN];
+char prevtoken;
+char out[OUTLEN];
+
+char buff[MAXBUFF];
+static size_t top;
+
+
+void err(char *s)
+{
+ printf("fatal error: %s\n", s);
+ exit(1);
+}
+
+int gettoken(void)
+{
+ char c, *p = token;
+ char getch(void);
+ void ungetch(char);
+
+ if (prevtoken == YES) {
+ prevtoken = NO;
+ return tokentype;
+ }
+
+ while(isblank(c = getch()))
+ ;
+
+ if (c == '/') { /* ignore comments */
+ if ((c = getch()) == '/') {
+ while ((c = getch()) != '\n')
+ ;
+ } else if (c == '*') {
+ while (getch() != '*' || (c = getch()) != '/')
+ if (c == '*')
+ ungetch('*');
+ return gettoken();
+ } else {
+ ungetch(c);
+ c = '/';
+ }
+ }
+
+ if (c == '(') {
+ if ((c = getch()) == ')') {
+ strcpy(token, "()");
+ return tokentype = PARENS;
+ } else {
+ ungetch(c);
+ return tokentype = '(';
+ }
+ } else if (c == '[') {
+ for (*p++ = '['; (*p++ = getch()) != ']';)
+ ;
+ *p = '\0';
+ return tokentype = BRACKETS;
+ } else if (isalpha(c)) {
+ for (*p++ = c; isalnum(c = getch());)
+ *p++ = c;
+ *p = '\0';
+ ungetch(c);
+ return tokentype = NAME;
+ } else {
+ return tokentype = c;
+ }
+}
+
+void ungetch(char c)
+{
+ if (top < MAXBUFF)
+ buff[top++] = c;
+ else
+ err("buff: stack overflow");
+}
+
+char getch(void)
+{
+ return (top > 0) ? buff[--top] : getchar();
+}
+
+int peaktoken()
+{
+ char type;
+
+ type = gettoken();
+ prevtoken = YES;
+ return type;
+}
+
+int main(void)
+{
+ int err;
+ char type;
+ char temp[OUTLEN + MAXTOKEN];
+
+ while (gettoken() != EOF) {
+ err = 0;
+ strcpy(out, token);
+ while ((type = gettoken()) != '\n') {
+ if (type == PARENS || type == BRACKETS ) {
+ strcat(out, token);
+ } else if (type == '*') {
+ if ((type = peaktoken()) == PARENS ||
+ type == BRACKETS)
+ sprintf(temp, "(*%s)", out);
+ else
+ sprintf(temp, "*%s", out);
+ strcpy(out, temp);
+ } else if (type == NAME) {
+ sprintf(temp, "%s %s", token, out);
+ out[0] = '\0'; /* rust sisters btfo */
+ strncat(out, temp, OUTLEN - 1);
+ } else {
+ err = 1;
+ printf("err: invalid input at %s\n", token);
+ while ((type = gettoken()) != '\n')
+ ;
+ ungetch('\n');
+ }
+ }
+
+ if (!err)
+ printf("%s\n", out);
+ }
+
+ return 0;
+}