aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorzlg <zlg@zlg.space>2013-02-06 02:21:09 -0600
committerzlg <zlg@zlg.space>2013-02-06 02:21:09 -0600
commitc278950b14248b9108dfbf9137ce283dadd5ac54 (patch)
treecba54540f06a4ba99d6047558381800e94d74aa7
parentFirst crack at 1-24 (diff)
downloadknr-c278950b14248b9108dfbf9137ce283dadd5ac54.tar.gz
knr-c278950b14248b9108dfbf9137ce283dadd5ac54.tar.bz2
knr-c278950b14248b9108dfbf9137ce283dadd5ac54.tar.xz
knr-c278950b14248b9108dfbf9137ce283dadd5ac54.zip
Solve Exercise 1-24: C syntax checker
I'm glad to be done with chapter 1. It feels like I'm starting to gain momentum. I'm certainly looking forward to using higher level constructs like switch(), which I think is available in Chapter 2. So far so good!
Diffstat (limited to '')
-rw-r--r--1-24_syntax-checker.c232
1 files changed, 122 insertions, 110 deletions
diff --git a/1-24_syntax-checker.c b/1-24_syntax-checker.c
index f56e4f7..239999f 100644
--- a/1-24_syntax-checker.c
+++ b/1-24_syntax-checker.c
@@ -15,135 +15,147 @@
*
* I can tackle this one the same way I tackled the previous exercise: with a
* FSM. The trick is in catching the mismatched levels.
+ *
+ * Post-solution note: switch() would've made this MUCH shorter...
*/
-/* Create our nestable counts. These keep track of each type of syntax
- * character. */
-#define PARENS 0
-#define BRACKETS 1
-#define BRACES 2
-
-/* Create our states, which tell the parser what's going on. INSQ, INDQ, and
- * INCM are much larger so it's easier to tell what's what. If I were better
- * versed in binary operations, this could use less RAM. I intend to revisit
- * this later on. */
-#define OUT 0
-#define INPR 1
-#define INBK 2
-#define INBC 3
-#define INSQ 100
-#define INDQ 1000
-#define INCM 10000
-
char c;
-int counts[3];
-int state, i, linenr;
-int main() {
- for (i = 0; i < 3; ++i) {
- counts[i] = 0;
- }
+/* Set the state/count variables */
+int linenr, parens, brackets, braces, singqs, dubqs, escapes, mcomms, scomms = 0;
- state = OUT;
+int main() {
linenr = 1;
// Begin streaming!
while ((c = getchar()) != EOF) {
- if (c == '\n') {
- linenr += 1;
- if (state >= INSQ) {
- break;
+ if (scomms == 1) {
+ if (c == '\n') {
+ scomms--;
+ } else {
+ continue;
}
- }
- if (c == '(') {
- counts[PARENS] += 1;
- state += INPR;
- }
- if (c == ')') {
- counts[PARENS] -= 1;
- state -= INPR;
- if (counts[PARENS] < 0) {
- break;
+ } else if (mcomms == 1) {
+ if (c == '*') {
+ if (getchar() == '/') {
+ mcomms--;
+ }
}
- }
- if (c == '[') {
- counts[BRACKETS] += 1;
- state += INBK;
- }
- if (c == ']') {
- counts[BRACKETS] -= 1;
- state -= INBK;
- if (counts[BRACKETS] < 0) {
- break;
+ } else {
+ // Check for escape sequences
+ if (c == '\\') {
+ escapes++;
+ c = getchar();
+ // This does not detect all sequences; just the ones covered in Chapter 1.
+ if (c != '\\' || c != 't' || c != '\'' || c != '"' || c != 'n' || c != 'b' || c != '0') {
+ break;
+ } else {
+ escapes--;
+ }
}
- }
- if (c == '{') {
- counts[BRACES] += 1;
- state += INBC;
- }
- if (c == '}') {
- counts[BRACES] -= 1;
- state -= INBC;
- if (counts[BRACES] < 0) {
- break;
+ // Newline behavior
+ if (c == '\n') {
+ if (singqs > 0 || dubqs > 0) {
+ break;
+ }
+ linenr += 1;
}
- }
- if (c == '"' && state > INDQ) {
- state -= INDQ;
- if (state < 0) {
- break;
+ // Parentheses
+ if (c == '(') {
+ parens += 1;
}
- }
- if (c == '"' && state < INSQ) {
- state += INDQ;
- if (state >= (INDQ * 2)) {
- break;
+ if (c == ')') {
+ parens -= 1;
+ if (parens < 0) {
+ break;
+ }
}
- }
- if (c == '\'' && state > INSQ && state < INDQ) {
- state -= INSQ;
- if (state < 0) {
- break;
+ // Brackets
+ if (c == '[') {
+ brackets += 1;
}
- }
- if (c == '\'' && state < INSQ) {
- state += INSQ;
- if (state >= (INSQ * 2)) {
- break;
+ if (c == ']') {
+ brackets--;
+ if (brackets < 0) {
+ break;
+ }
+ }
+ // Braces
+ if (c == '{') {
+ braces++;
+ }
+ if (c == '}') {
+ braces--;
+ if (braces < 0) {
+ break;
+ }
+ }
+ // Double quotes
+ if (c == '"') {
+ if (dubqs == 0) {
+ dubqs++;
+ } else {
+ dubqs--;
+ }
+ }
+ // Single quotes, which are only checked outside of doubles.
+ if (c == '\'' && dubqs == 0) {
+ if (singqs == 0) {
+ singqs++;
+ } else {
+ singqs--;
+ }
+ }
+ // Comment detection
+ if (c == '/') {
+ if (getchar() == '*') {
+ mcomms++;
+ }
+ if (getchar() == '/') {
+ scomms++;
+ }
}
}
}
- if (state != 0) {
- printf("SYNTAX ERROR: ");
-
- if (state >= INSQ && state < INDQ) {
- printf("Unclosed single quote on line %d!\n", linenr);
- return 1;
- }
- if (counts[PARENS] > 0) {
- printf("Unclosed parenthesis on line %d!\n", linenr);
- return 1;
- }
- if (counts[PARENS] < 0) {
- printf("Too many close parentheses on line %d!\n", linenr);
- return 1;
- }
- if (counts[BRACKETS] > 0) {
- printf("Unclosed brackets on line %d!\n", linenr);
- return 1;
- }
- if (counts[BRACKETS] < 0) {
- printf("Too many close brackets on line %d!\n", linenr);
- return 1;
- }
- if (counts[BRACES] > 0) {
- printf("Unclosed braces on line %d!\n", linenr);
- return 1;
- }
- if (counts[BRACES] < 0) {
- printf("Too many close braces on line %d!\n", linenr);
- return 1;
- }
+ if (escapes > 0) {
+ printf("Invalid escape sequence on line %d!\n", linenr);
+ return 1;
+ }
+ if (singqs > 0) {
+ printf("Unclosed single quote on line %d!\n", linenr);
+ return 1;
+ }
+ if (dubqs > 0) {
+ printf("Unclosed double quote on line %d!\n", linenr);
+ return 1;
+ }
+ if (brackets > 0) {
+ printf("Unclosed brackets on line %d!\n", linenr);
+ return 1;
+ }
+ if (brackets < 0) {
+ printf("Too many close brackets on line %d!\n", linenr);
+ return 1;
+ }
+ if (parens > 0) {
+ printf("Unclosed parenthesis on line %d!\n", linenr);
+ return 1;
+ }
+ if (parens < 0) {
+ printf("Too many close parentheses on line %d!\n", linenr);
+ return 1;
+ }
+ if (braces > 0) {
+ printf("Unclosed braces on line %d!\n", linenr);
+ return 1;
+ }
+ if (braces < 0) {
+ printf("Too many close braces on line %d!\n", linenr);
+ return 1;
+ }
+ if (mcomms == 1) {
+ printf("Unclosed comment at end of file!\n");
+ return 1;
}
printf("All clean.\n");
stash/commit/src/vgstash_cli.py?h=v0.3b5&id=9de7357d758ab2253061ce7d68fb3942b52ee7a3&follow=1'>cli: change "Status" heading to "Progress"zlg2-36/+40 2018-09-29Bump to 0.3alpha5 for PyPIzlg1-1/+1 2018-09-29cli: Add pretty printing to 'list' commandzlg3-17/+107 Also add the "--width" option to specify the maximum width of the table. 2018-09-08setup.py: Bump to alpha4 for PyPIzlg1-1/+1 2018-09-08cli: add '--raw' option to list commandzlg2-9/+45 Add '--raw' option to the list command, in addition to proper note expansion. Newline characters in notes are escaped to be friendly to scripting. This option may be shortened to '-r' at the user's convenience. In raw output mode, the information is formatted in plain pipe-delimited strings, one line per row: title|system|ownership|progress|notes ownership and progress are printed in their numeric form, consistent with the OWNERSHIP and PROGRESS dictionaries in the vgstash package. An empty notes field will result in a line ending with a pipe and no whitespace following it. 2018-09-08Add remaining filters to vgstash packagezlg1-2/+11 2018-09-04Update LICENSE to match setup.pyzlg1-80/+67 Whoops. 2018-09-03Branch off from master with pytest, tox, clickzlg16-778/+779 This commit is huge, but contains everything needed for a "proper" build system built on pytest + tox and a CLI built with click. For now, this branch will contain all new vgstash development activity until it reaches feature parity with master. The CLI is installed to pip's PATH. Only the 'init', 'add', and 'list' commands work, with only two filters. This is pre-alpha software, and is therefore not stable yet. 2018-03-18Flesh out filter types and ownership statuszlg3-82/+144 It's time for a refactor to a module; the functionality and interface are clashing. 2018-03-18README.mdown: break line correctlyzlg1-1/+1 2018-03-18add 'playlog' list filterzlg2-2/+9 This filter is used to get an idea of which games you're currently playing through, so you can prioritize games to play when you're bored and detect it when you've beaten a game but haven't marked it as such. 2018-03-13Update helpers a bitzlg1-2/+9 At present, user modification is needed to make these seamless. vgup() may need to be axed in favor of telling the user to make an alias. 2018-03-13Make VGSTASH_DB_LOCATION point to a filezlg2-21/+20 It used to point to a directory, which would then look for .vgstash.db. This behavior was kind of backwards and I don't remember why I did it that way. This change gives users more control over where they put their DB. Be sure to update your environment variable if you have it set! 2016-11-18Remove settings from helpers.shZe Libertine Gamer1-5/+0 Sourcing them in .bash_profile screws up login if they're set. 2016-11-15Correct phrasing in README.Ze Libertine Gamer1-4/+4 2016-11-13DerpZe Libertine Gamer1-0/+1 2016-11-03Improve error handling in shell scriptsZe Libertine Gamer4-3/+23 2016-10-24Correct run_again, add recursionZe Libertine Gamer1-0/+4 Loops and functions -- oh my, what a useful combination. :) 2016-10-21Add quotes to correct behavior for arglistZe Libertine Gamer1-1/+1 2016-10-14updater.sh: add recursion, error handlingZe Libertine Gamer1-43/+101 2016-10-14Correct pipe-handling behaviorZe Libertine Gamer1-1/+9 2016-10-12Clarify a method to move between platformsZe Libertine Gamer1-2/+5 Also correct a typo.