Jump to content

Check out our Community Blogs

Register and join over 40,000 other developers!

Recent Status Updates

View All Updates

- - - - -

Syntax checker


  • Please log in to reply
3 replies to this topic

#1 restin84


    CC Resident

  • Advanced Member
  • PipPipPipPip
  • 88 posts
  • Programming Language:C, Java, C++
  • Learning:PHP, Python, Assembly

Posted 30 October 2009 - 02:21 PM

Hey guys. I have an assignment for my comp sci class. We are writing a syntax checker( matching parens, brackets, braces, etc.). Just wondering if anyone had any input or suggestions. Gotta use a stack data type.
  • 0

#2 TkTech


    The Crazy One

  • Retired Mod
  • PipPipPipPipPipPipPip
  • 1144 posts
  • Location:Ottawa, Ontario

Posted 30 October 2009 - 06:35 PM

You'll want to write a parser, usually top-down left-right. Its the long way, but its the right way. You'll end up with a very powerful program. All compilers feature a component known as the parser, which is used to verify the syntax and generate a symbol tree. There are also many compiler courses for university that publish their material online - a quick google showed me dozens of papers, presentations, tutorials, and references.
  • 0
Helpful CODECALL Links: Join Us, Guidelines, FAQ, Post a Tutorial

#3 manux


    CC Addict

  • Just Joined
  • PipPipPipPipPip
  • 211 posts

Posted 01 November 2009 - 05:39 AM

Making the lexer is the best part :D
There are alot of examples that use stack, but I think the best thing is just to parse your code twice, the first time you just count how many tokens you have(and possibly search for syntax errors), and the second time, you actually make a token array and store them.
  • 0

#4 WingedPanther73


    A spammer's worst nightmare

  • Moderator
  • 17757 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others

Posted 02 November 2009 - 09:23 AM

Short answer:
walk through the code one character at a time. For every (, {, [, etc encountered, push it on the stack, for every ), }, ], etc, encountered, pop the stack and look for a match. If the code is valid, you will have a match every time, and not have anything left on the stack when done. Handling strings will be slightly trickier.
  • 0

Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

My MineCraft server site: http://banishedwings.enjin.com/

Also tagged with one or more of these keywords: syntax

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download