Adding Error Checking to Macros

Adding Error Checking to Macros - CS1102: Adding Error...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
CS1102: Adding Error Checking to Macros Kathi Fisler, WPI October 8, 2004 1 Typos in State Machines The point of creating macros for state machines is to hide language details from the programmer. Ideally, a programmer shouldn’t need to know anything more than the macro input syntax in order to write down usable state machines. Recall our macro for converting the clean state machine syntax into the structure-based language: ( define-syntax monitor1 ( syntax-rules ( - > : ) [( monitor1 initname ( curr-state : ( label - > next-state ) ...) ...) ( make-monitor initname ( list ( make-state curr-state ( list ( make-trans label next-state ) ...)) ...))])) This macro is designed to work with the following interpreter for testing state machine monitors against sequences of inputs: ;; interp-monitor : monitor list[symbol] symbol ;; run monitor on samples, returning ’okay or ’error ( define ( interp-monitor a-monitor samples ) ( run-monitor ( monitor-init-state a-monitor ) samples ( monitor-states a-monitor ))) ;; run-monitor : symbol list[symbol] list[states] symbol ;; run monitor on samples from current state, returning ’okay or ’error ( define ( run-monitor curr-state samples all-states ) ( cond [( empty? samples ) ’ okay ] [( cons? samples ) ( let ([ next-state ( find-next-state curr-state ( first samples ) all-states )]) ( cond [( boolean? next-state ) ’ error ] [ else ( run-monitor next-state ( rest samples ) all-states )]))])) ;; find-next-state : symbol symbol list[state] symbol or false ;; finds name of next-state in transition from given state (first arg) on given input/guard (second arg) ( define ( find-next-state curr-state label states ) ( let ([ state ( first ( filter ( lambda ( st ) ( symbol = ? curr-state ( state-name st ))) states ))]) ( let ([ trans ( filter ( lambda ( tr ) ( symbol = ? label ( trans-guard tr ))) ( state-transitions state ))]) 1
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
( cond [( empty? trans ) false ] [ else ( trans-next-state ( first trans ))])))) Imagine that a programmer uses the macro as shown below, but makes a typo in the first transition to ’schemeis- green (left out one of the ”e”s in green). What happens when the programmer tries to test the state machine? ( define buggy-TL-monitor ( monitor1 is-red ( is-red : ( green - > is-gren ) ( red - > is-red )) ( is-green : ( yellow - > is-yellow ) ( green - > is-green )) ( is-yellow : ( red - > is-red )))) > ( interp-monitor buggy-TL-monitor ( list red green yellow green )) first: expects argument of type < non-empty list > ; given () This error comes from the find-next-state function, specifically the call to first used to get a value for the state variable. Knowing how the macro and interpreter work, this error makes sense: the first sample (’ red ) pulled out next- state is-gren , which got passed as the current state to find-next-state
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 02/06/2008 for the course CS 1102 taught by Professor Fisler during the Fall '07 term at WPI.

Page1 / 6

Adding Error Checking to Macros - CS1102: Adding Error...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online