Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- int main () {
- int c,next,state,read;
- enum {program = 1, comment, line_comment, squote,dquote,s_escape,d_escape}; /*s_escape is escape from a single quote and d_escape from double*/
- state = program; //this is the default state
- while ((c = getchar()) != EOF) {
- if (c == '\'') { /*if you come across a single quote*/
- if (state == program)
- state = squote;
- else if (state == squote)
- state = program;
- else if (state == dquote)
- state = dquote;
- else if (state == s_escape)
- state = squote;
- else if (state == d_escape)
- state = dquote;
- else
- ;
- if (state != comment && state != line_comment)
- putchar(c);
- }
- else if (c == '"') {
- if (state == program)
- state = dquote;
- else if (state == squote)
- state = squote;
- else if (state == dquote)
- state = program;
- else if (state == s_escape)
- state = squote;
- else if (state == d_escape)
- state = dquote;
- else
- ;
- if (state != comment && state != line_comment)
- putchar(c);
- }
- else if (c == '\\') {
- if (state == squote)
- state = s_escape;
- else if (state == dquote)
- state = d_escape;
- else if (state == s_escape)
- state = squote;
- else if (state == d_escape)
- state = dquote;
- else if (state == program)
- state = program;
- else
- ;
- if (state != comment && state != line_comment)
- putchar(c);
- }
- else if (c == '/') {
- if (state == program) {
- if ((next = getchar()) == '*')
- state = comment;
- else if (next == '/') {
- state = line_comment; //this is an example of a line_comment
- while ((read = getchar()) != '\n')
- ;
- if (read == '\n')
- putchar('\n');
- state = program;
- }
- else {
- state = program;
- putchar('/');
- putchar(next);
- }
- }
- else if (state == squote || state == dquote)
- putchar('/');
- else if (state == s_escape) {
- state = squote;
- putchar('/');
- }
- else if (state == d_escape) {
- state = dquote;
- putchar('/');
- }
- else
- ;
- }
- else if (c == '*') {
- if (state == comment) {
- if ((next = getchar()) == '/')
- state = program;
- continue;
- }
- else if (state == squote || state == dquote || state == program)
- putchar('*');
- else if (state == s_escape) {
- state = squote;
- putchar('*');
- }
- else if (state == d_escape) {
- state = dquote;
- putchar('*');
- }
- else
- ;
- }
- else {
- if (state != comment && state != line_comment) {
- putchar(c);
- if (state == s_escape)
- state = squote;
- else if (state == d_escape)
- state = dquote;
- }
- }
- }
- return 0;
- }
- //another line comment
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement